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();
}