獲取本地IP和mac等資訊

weixin_34391854發表於2014-07-15

 

1獲取mac

protected string getHostMacName()
{
string mac = "";
ManagementClass mc;
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;
}

2.獲取IP
static string GetLocalIp()
{
string hostname = Dns.GetHostName();//得到本機名
//IPHostEntry localhost = Dns.GetHostByName(hostname);//方法已過期,只得到IPv4的地址
IPHostEntry localhost = Dns.GetHostEntry(hostname);
IPAddress localaddr = localhost.AddressList[0];
return localaddr.ToString();
}

相關文章