C# 获取硬件参数的实现方法
C#获取硬件参数的实现方法
示例代码:
privatestaticstringGetIdentifier(stringwmiClass,stringwmiProperty,stringwmiMustBeTrue) { stringresult=""; System.Management.ManagementClassmc=newSystem.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollectionmoc=mc.GetInstances(); foreach(System.Management.ManagementObjectmoinmoc) { if(mo[wmiMustBeTrue].ToString()=="True") { //Onlygetthefirstone if(result=="") { try { result=mo[wmiProperty].ToString(); break; } catch { } } } } returnresult; } privatestaticstringGetIdentifier(stringwmiClass,stringwmiProperty) { stringresult=""; System.Management.ManagementClassmc=newSystem.Management.ManagementClass(wmiClass); System.Management.ManagementObjectCollectionmoc=mc.GetInstances(); foreach(System.Management.ManagementObjectmoinmoc) { //Onlygetthefirstone if(result=="") { try { result=mo[wmiProperty].ToString(); break; } catch { } } } returnresult; } //cpuid GetIdentifier("Win32_Processor","UniqueId"); //processorid GetIdentifier("Win32_Processor","ProcessorId"); //processorname GetIdentifier("Win32_Processor","Name"); //Manufacturer GetIdentifier("Win32_Processor","Manufacturer"); //BIOSIdentifier privatestaticstringGetBiosId() { returnGetIdentifier("Win32_BIOS","Manufacturer") +GetIdentifier("Win32_BIOS","SMBIOSBIOSVersion") +GetIdentifier("Win32_BIOS","IdentificationCode") +GetIdentifier("Win32_BIOS","SerialNumber") +GetIdentifier("Win32_BIOS","ReleaseDate") +GetIdentifier("Win32_BIOS","Version"); } //MainphysicalharddriveID privatestaticstringGetDiskId() { returnGetIdentifier("Win32_DiskDrive","Model") +GetIdentifier("Win32_DiskDrive","Manufacturer") +GetIdentifier("Win32_DiskDrive","Signature") +GetIdentifier("Win32_DiskDrive","TotalHeads"); } //MotherboardID privatestaticstringGetBaseId() { returnGetIdentifier("Win32_BaseBoard","Model") +GetIdentifier("Win32_BaseBoard","Manufacturer") +GetIdentifier("Win32_BaseBoard","Name") +GetIdentifier("Win32_BaseBoard","SerialNumber"); } //PrimaryvideocontrollerID privatestaticstringGetVideoId() { returnGetIdentifier("Win32_VideoController","DriverVersion") +GetIdentifier("Win32_VideoController","Name"); } //FirstenablednetworkcardID privatestaticstringGetMacId() { returnGetIdentifier("Win32_NetworkAdapterConfiguration","MACAddress","IPEnabled"); }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!