这里介绍下如何利用C#获取网卡的物理地址(MAC Address).
首先要引用using System.Management;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//get the MAC address public static string[] getMAC() { ManagementClass mc=new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc=mc.GetInstances(); string[] MacAddress=new string[2]; int ii=0; foreach(ManagementObject mo in moc) { if((bool)mo["IPEnabled"]==true) { MacAddress[ii]=mo["MacAddress"].ToString(); ii++; } mo.Dispose(); } return MacAddress; } |