C#获取本地计算机名IP,Mac地址
string s="",mac="";
//
//name
//
string hostInfo = Dns.GetHostName();
//
//IP
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
for (int i = 0; i < addressList.Length; i ++)
{
s += addressList[i].ToString();
}
//
//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();
}
txtName.Text=hostInfo;
txtIp.Text=s;
txtMac.Text=mac;
button1.Enabled=false;
button2.Focus();
//
//name
//
string hostInfo = Dns.GetHostName();
//
//IP
System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
for (int i = 0; i < addressList.Length; i ++)
{
s += addressList[i].ToString();
}
//
//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();
}
txtName.Text=hostInfo;
txtIp.Text=s;
txtMac.Text=mac;
button1.Enabled=false;
button2.Focus();