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");
}
相关文章
WMI获取硬件信息封装函数方法(联想台式机出厂编号 CPUID BIOS序列号 硬盘信息 显卡信息 MAC地址)
这篇文章主要介绍了WMI获取硬件信息的方法,硬件信息有:联想台式机出厂编号 CPUID BIOS序列号 硬盘信息 显卡信息 MAC地址...2020-06-25- 这篇文章主要介绍了c#使用wmi查询usb设备信息示例,大家参考使用吧...2020-06-25
- 这篇文章主要介绍了详解WMI RPC 服务器不可用的解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- WMI提供了一套内置在Microsoft Windows操作系统中的丰富的系统管理服务,可以在有大量的应用程序、服务和设备的系统中提供全方位的管理功能。它允许应用程序的开发者,使用简单的、一致的机制,去查询企业中的任一台计算机上的信息,或是进行系统配置...2020-06-25
- 这篇文章主要介绍了C#编程获取各种电脑硬件信息的方法,结合实例形式分析了C#获取电脑CPU、主板、硬盘、BIOS编号等信息的相关操作技巧与注意事项,需要的朋友可以参考下...2020-06-25
- 此程序纯批处理,且纯净绿色,也不会在生成文件中包含我的个人信息,大家不要随意修改里面的代码结构,很可能自己电脑没问题,到了别的电脑就会获取不准...2020-06-30
- 我们应该都知道System.Management提供的类可以用于读取本地计算机设备的各种数据,下面这篇文章主要给大家介绍了关于如何用C#获取计算机详细的软件和硬件信息的相关资料,需要的朋友可以参考下...2022-12-30