C#自用获取本机IP,MAC方法

//using System.Diagnostics;
//using System.IO;
//using System.Net.NetworkInformation;
//using System.Net.Sockets;

 

        #region 获得本机MAC地址

        /// <summary>  
        /// 通过DOS命令获得MAC地址  
        /// </summary>  
        /// <returns></returns>
        public string GetMacAddressByDos()
        {
            string macAddress = "";
            Process p = null;
            StreamReader reader = null;
            try
            {
                ProcessStartInfo start = new ProcessStartInfo("cmd.exe");

                start.FileName = "ipconfig";
                start.Arguments = "/all";

                start.CreateNoWindow = true;

                start.RedirectStandardOutput = true;

                start.RedirectStandardInput = true;

                start.UseShellExecute = false;

                p = Process.Start(start);

                reader = p.StandardOutput;

                string line = reader.ReadLine();

                while (!reader.EndOfStream)
                {
                    if (line.ToLower().IndexOf("physical address") > 0 || line.ToLower().IndexOf("物理地址") > 0)
                    {
                        int index = line.IndexOf(":");
                        index += 2;
                        macAddress = macAddress + line.Substring(index) + ",";
                    }
                    line = reader.ReadLine();
                }
            }
            catch
            {

            }
            finally
            {
                if (p != null)
                {
                    p.WaitForExit();
                    p.Close();
                }
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return macAddress.Equals("") ? macAddress : macAddress.Substring(0, macAddress.Length - 1);
        }

        /// <summary>  
        /// 通过网络适配器获取MAC地址  
        /// </summary>  
        /// <returns></returns>  
        public string GetMacAddressByNetworkInformation()
        {
            string macAddress = "";
            string tempMacAddress = "";
            try
            {
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in nics)
                {
                    if (!adapter.GetPhysicalAddress().ToString().Equals(""))
                    {
                        tempMacAddress = adapter.GetPhysicalAddress().ToString();
                        for (int i = 1; i < 6; i++)
                        {
                            tempMacAddress = tempMacAddress.Insert(3 * i - 1, "-");
                        }
                        macAddress = macAddress + tempMacAddress + ",";
                    }
                }

            }
            catch
            {
            }
            return macAddress.Equals("") ? macAddress : macAddress.Substring(0, macAddress.Length - 1);
        }

        #endregion

        #region 获得本机IPV4地址

        /// <summary>  
        /// 通过DOS命令获得IP地址  
        /// </summary>  
        /// <returns></returns>
        public string GetIPAddressByDos()
        {
            string ipAddress = "";
            Process p = null;
            StreamReader reader = null;
            try
            {
                ProcessStartInfo start = new ProcessStartInfo("cmd.exe");

                start.FileName = "ipconfig";
                start.Arguments = "/all";

                start.CreateNoWindow = true;

                start.RedirectStandardOutput = true;

                start.RedirectStandardInput = true;

                start.UseShellExecute = false;

                p = Process.Start(start);

                reader = p.StandardOutput;

                string line = reader.ReadLine();

                while (!reader.EndOfStream)
                {
                    if (line.ToLower().IndexOf("ip address") > 0 || line.ToLower().IndexOf("IP地址") > 0)
                    {
                        int index = line.IndexOf(":");
                        index += 2;
                        ipAddress = ipAddress + line.Substring(index) + ",";
                    }
                    line = reader.ReadLine();
                }
            }
            catch
            {

            }
            finally
            {
                if (p != null)
                {
                    p.WaitForExit();
                    p.Close();
                }
                if (reader != null)
                {
                    reader.Close();
                }
            }
            return ipAddress.Equals("") ? ipAddress : ipAddress.Substring(0, ipAddress.Length - 1);
        }

        /// <summary>  
        /// 通过网络适配器获取IP地址  
        /// </summary>  
        /// <returns></returns>  
        public string GetIPAddressByNetworkInformation()
        {
            string ipAddress = "";
            NetworkInterface[] NetworkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
            foreach (NetworkInterface NetworkIntf in NetworkInterfaces)
            {
                IPInterfaceProperties IPInterfaceProperties = NetworkIntf.GetIPProperties();
                UnicastIPAddressInformationCollection UnicastIPAddressInformationCollection = IPInterfaceProperties.UnicastAddresses;
                foreach (UnicastIPAddressInformation UnicastIPAddressInformation in UnicastIPAddressInformationCollection)
                {
                    if (UnicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        //排除本机自由IP
                        if (!UnicastIPAddressInformation.Address.ToString().Equals("127.0.0.1"))
                            ipAddress = ipAddress + UnicastIPAddressInformation.Address + ",";
                    }
                }
            }
            return ipAddress.Equals("") ? ipAddress : ipAddress.Substring(0, ipAddress.Length - 1);
        } 

        #endregion

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值