WMI获取硬件信息封装函数方法(联想台式机出厂编号 CPUID BIOS序列号 硬盘信息 显卡信息 MAC地址)
今天玩了一把wmi,查询了一下电脑的硬件信息,感觉很多代码都是可以提取出来的,就自己把那些公共部分提出出来,以后如果要获取某部分的硬件信息就不用写一个一个的函数,比如获取mac地址就写一个获取mac地址的函数,获取cpu 信息就写一个获取cpu信息的函数,太麻烦了
如下是函数代码:
private static string identifier(string wmiclass, string wmiproperty, string wmimustbetrue)
{
string result = "";
system.management.managementclass mc = new system.management.managementclass(wmiclass);
system.management.managementobjectcollection moc = mc.getinstances();
foreach (system.management.managementobject mo in moc)
{
if (mo[wmimustbetrue].tostring() == "true")
{
if (result == "")
{
try
{
result = mo[wmiproperty].tostring();
break;
}
catch
{
}
}
}
}
return result;
}
private static string identifier(string wmiclass, string wmiproperty)
{
string result = "";
system.management.managementclass mc = new system.management.managementclass(wmiclass);
system.management.managementobjectcollection moc = mc.getinstances();
foreach (system.management.managementobject mo in moc)
{
if (result == "")
{
try
{
result = mo[wmiproperty].tostring();
break;
}
catch
{
}
}
}
return result;
}
获取cpuid
private static string cpuid()
{
string retval = identifier("win32_processor", "uniqueid"); //cpuid
retval += identifier("win32_processor", "processorid");
retval += identifier("win32_processor", "name"); //处理器名称
retval += identifier("win32_processor", "manufacturer"); //处理器制造商
retval +=identifier("win32_processor", "maxclockspeed"); //最大时钟频率
return retval;
}
获取bios信息,其中bios序列号就是联想台式机的出厂编号,我看联想的保修页面里的自动获取主机编号应该也是调用这个"win32_bios"的 "serialnumber
报修页面网址:http://support1.lenovo.com.cn/lenovo/wsi/wsbx/lenovo/#minarepairinfo
//bios信息
private static string biosid()
{
return identifier("win32_bios", "manufacturer") //bios制造商名称
+ identifier("win32_bios", "smbiosbiosversion") //
+ identifier("win32_bios", "identificationcode") //
+ identifier("win32_bios", "serialnumber") //bios序列号
+ identifier("win32_bios", "releasedate") //出厂日期
+ identifier("win32_bios", "version"); //版本号
}
获取硬盘信息:
private static string diskid()
{
return identifier("win32_diskdrive", "model") //模式
+ identifier("win32_diskdrive", "manufacturer") //制造商
+ identifier("win32_diskdrive", "signature") //签名
+ identifier("win32_diskdrive", "totalheads"); //扇区头
}
获取显卡信息:
private static string videoid()
{
return identifier("win32_videocontroller", "driverversion")
+ identifier("win32_videocontroller", "name");
}
获取网卡mac地址信息:
private static string macid()
{
return identifier("win32_networkadapterconfiguration", "macaddress", "ipenabled");
}