需要using System.Management;
/// <summary>
/// Get LocalHost MAC Address
/// </summary>
/// <returns></returns>
public string GetHostMac()
{
ManagementClass mc;
string mac = string.Empty;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["IPEnabled"].ToString() == "True")
{
mac = mo["MacAddress"].ToString();
}
}
return mac;
}
注意:
工程里面要添加引用System.Management,不是光是代码里面,方法如下:
右击工程,添加引用,.net标签,System.Management,确定。