日期:2011-06-10  浏览次数:20475 次

string s="",mac="";
// 得到主机名
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();
}

Console.Write("主机名:" + hostInfo);
Console.Write("IP地址:" + s);
Console.Write("MAC地址:" + mac);