利用C#獲取計算機名,IP,MAC信息,如下為源代碼:
代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Management;
namespace Wenanry.Net
{
/// <summary>
/// 獲取計算機系統信息
/// </summary>
public class ManagementSystemInfo
{
/// <summary>
/// 獲取主機名
/// </summary>
/// <returns></returns>
public string HostName
{
get
{
string hostname = Dns.GetHostName();
return hostname;
}
}
/// <summary>
/// 獲取IP地址
/// </summary>
/// <returns></returns>
public List<string> GetIPList()
{
List<string> ipList = new List<string>();
IPAddress[] addressList = Dns.GetHostEntry(this.HostName).AddressList;
for (int i = 0; i < addressList.Length; i++)
{
ipList.Add(addressList[i].ToString());
}
return ipList;
}
/// <summary>
/// 獲取Mac地址
/// </summary>
/// <returns></returns>
public List<string> getMacList()
{
List<string> macList = new List<string>();
ManagementClass mc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["IPEnabled"].ToString() == "True")
macList.Add(mo["MacAddress"].ToString());
}
return macList;
}
}
}