C# 获取 Ip Mac

public string GetMacAdd(string str = "")
        {
            string mac = null;
            string strMac = string.Empty;
            try
            {
                ManagementObjectSearcher query = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection queryCollection = query.Get();
                foreach (ManagementObject mo in queryCollection)
                {
                    if (mo["IPEnabled"].ToString() == "True")
                    {
                        System.Array ar;
                        ar = (System.Array)(mo.Properties["IPAddress"].Value);
                        string ip = ar.GetValue(0).ToString();
                        //if (ip == str)
                        mac = mo["MacAddress"].ToString();
                        if (mac != null) return mac;

                    }
                }
            }
            catch (Exception ex)
            {
                //LogHelper.ErrorFormat("获取本机Mac出错:" + ex.Message);
            }
            return (mac);
        }

 public string GetLocalIP()
        {
            try
            {
                string HostName = Dns.GetHostName(); //得到主机名
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for (int i = 0; i < IpEntry.AddressList.Length; i++)
                {
                    //从IP地址列表中筛选出IPv4类型的IP地址
                    //AddressFamily.InterNetwork表示此IP为IPv4,
                    //AddressFamily.InterNetworkV6表示此地址为IPv6类型
                    if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                    {
                        return IpEntry.AddressList[i].ToString();
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
               // LogHelper.ErrorFormat("获取本机IP出错:" + ex.Message);
                return "";
            }
        }
注:需要引用
using System.Management;
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
原文地址:https://www.cnblogs.com/YYkun/p/15502243.html