当前位置: 编程技术>.net/c#/asp.net
C#获取计算机名,IP,MAC信息实现代码
来源: 互联网 发布时间:2014-10-15
本文导语: 利用C#获取计算机名,IP,MAC信息,如下为源代码: 代码如下: using System; using System.Collections.Generic; using System.Text; using System.Net; using System.Management; namespace Wenanry.Net { /// /// 获取计算机系统信息 /// public class ManagementSystemInfo { //...
利用C#获取计算机名,IP,MAC信息,如下为源代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Management;
namespace Wenanry.Net
{
///
/// 获取计算机系统信息
///
public class ManagementSystemInfo
{
///
/// 获取主机名
///
///
public string HostName
{
get
{
string hostname = Dns.GetHostName();
return hostname;
}
}
///
/// 获取IP地址
///
///
public List GetIPList()
{
List ipList = new List();
IPAddress[] addressList = Dns.GetHostEntry(this.HostName).AddressList;
for (int i = 0; i < addressList.Length; i++)
{
ipList.Add(addressList[i].ToString());
}
return ipList;
}
///
/// 获取Mac地址
///
///
public List getMacList()
{
List macList = new List();
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;
}
}
}
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Management;
namespace Wenanry.Net
{
///
/// 获取计算机系统信息
///
public class ManagementSystemInfo
{
///
/// 获取主机名
///
///
public string HostName
{
get
{
string hostname = Dns.GetHostName();
return hostname;
}
}
///
/// 获取IP地址
///
///
public List GetIPList()
{
List ipList = new List();
IPAddress[] addressList = Dns.GetHostEntry(this.HostName).AddressList;
for (int i = 0; i < addressList.Length; i++)
{
ipList.Add(addressList[i].ToString());
}
return ipList;
}
///
/// 获取Mac地址
///
///
public List getMacList()
{
List macList = new List();
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;
}
}
}