Today I played WMI and checked the hardware information of the computer. I felt that many codes could be extracted, so I put forward the public parts myself. If I want to obtain the hardware information of a certain part, I don’t have to write functions one by one. For example, if I get the MAC address, I write a function to obtain the MAC address, and if I get the CPU information, I write a function to obtain the CPU information. It’s too troublesome.
The following is the function code:
private static string identifier(string wmiClass, string wmiProperty, string wmiMustBeTrue)
{
string result = "";
mc = new (wmiClass);
moc = ();
foreach ( 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 = "";
mc = new (wmiClass);
moc = ();
foreach ( mo in moc)
{
if (result == "")
{
try
{
result = mo[wmiProperty].ToString();
break;
}
catch
{
}
}
}
return result;
}
Get CPUID
private static string cpuId()
{
string retVal = identifier("Win32_Processor", "UniqueId"); //CPUID
retVal += identifier("Win32_Processor", "ProcessorId");
retVal += identifier("Win32_Processor", "Name"); //Processor name
retVal += identifier("Win32_Processor", "Manufacturer"); //Processor Manufacturer
retVal +=identifier("Win32_Processor", "MaxClockSpeed"); //Maximum clock frequency
return retVal;
}
Get BIOS information, where the BIOS serial number is the factory number of Lenovo desktop. I see that the automatic access to the host number in Lenovo's warranty page should also call the "SerialNumber of "Win32_BIOS".
Repair page URL: /lenovo/wsi/wsbx/lenovo/#minarepairInfo
//BIOS information
private static string biosId()
{
return identifier("Win32_BIOS", "Manufacturer") �
+ identifier("Win32_BIOS", "SMBIOSBIOSVersion") //
+ identifier("Win32_BIOS", "IdentificationCode") //
+ identifier("Win32_BIOS", "SerialNumber") //BIOS serial number
+ identifier("Win32_BIOS", "ReleaseDate") �
+ identifier("Win32_BIOS", "Version"); �
}
Get hard disk information:
private static string diskId()
{
return identifier("Win32_DiskDrive", "Model")
+ identifier("Win32_DiskDrive", "Manufacturer") //Manufacturer
+ identifier("Win32_DiskDrive", "Signature") //Signature
+ identifier("Win32_DiskDrive", "TotalHeads"); //Sector header
}
Get graphics card information:
private static string videoId()
{
return identifier("Win32_VideoController", "DriverVersion")
+ identifier("Win32_VideoController", "Name");
}
Obtain network card MAC address information:
private static string macId()
{
return identifier("Win32_NetworkAdapterConfiguration", "MACAddress", "IPEnabled");
}