效果:
说明:获取本局域网的MAC地址(非本机的MAC地址)
代码:
1 /// <summary> 2 /// 获取网卡物理地址 3 /// </summary> 4 /// <returns></returns> 5 public static string getMAC() 6 { 7 string madAddr = null; 8 ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 9 ManagementObjectCollection moc2 = mc.GetInstances(); 10 foreach (ManagementObject mo in moc2) 11 { 12 if (Convert.ToBoolean(mo["IPEnabled"]) == true) 13 { 14 madAddr = mo["MacAddress"].ToString(); 15 madAddr = madAddr.Replace(':', '-'); 16 } 17 mo.Dispose(); 18 } 19 return madAddr; 20 }