C#中获取Ip和Mac地址

C#中获取Ip和Mac地址

        // 获取本机的IP和MAC
        public static void GetLocalMac(ref string ip, ref  string mac)
        {
            try
            {
                List<string> listIP = new List<string>();
                ManagementClass mcNetworkAdapterConfig = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc_NetworkAdapterConfig = mcNetworkAdapterConfig.GetInstances();
                foreach (ManagementObject mo in moc_NetworkAdapterConfig)
                {
                    string mServiceName = mo["ServiceName"] as string;

                    //过滤非真实的网卡  
                    if (!(bool)mo["IPEnabled"])
                    { continue; }
                    if (mServiceName.ToLower().Contains("vmnetadapter")
                 || mServiceName.ToLower().Contains("ppoe")
                 || mServiceName.ToLower().Contains("bthpan")
                 || mServiceName.ToLower().Contains("tap")
                 || mServiceName.ToLower().Contains("ndisip")
                 || mServiceName.ToLower().Contains("sinforvnic"))
                    { continue; }

                    if (mo["IPEnabled"].ToString() == "True")
                    {
                        mac = mo["MacAddress"].ToString().Replace(":", "-");
                    }
                    string[] mIPAddress = mo["IPAddress"] as string[];
                    ip = mIPAddress[0];
                    mo.Dispose();
                }

            }
            catch (Exception ex)
            {
                NLogLogger.Instance.Error(ex.ToString());
            }
        }

你可能感兴趣的:(C#,c#,ip,mvc)