C#获取计算机名,IP,MAC信息实现代码
程序员文章站
2023-12-09 23:47:51
利用c#获取计算机名,ip,mac信息,如下为源代码: 复制代码 代码如下: using system; using system.collections.generic;...
利用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;
}
}
}
复制代码 代码如下:
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;
}
}
}