C#获取计算机名,IP,MAC信息实现代码
利用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;
}
}
}