问题
string strProcessorId = string.Empty;
SelectQuery query = new SelectQuery("Win32_processor");
ManagementObjectSearcher search = new ManagementObjectSearcher(query);
foreach (ManagementObject info in search.Get())
{
strProcessorId = info["processorId"].ToString();
}
Console.WriteLine(strProcessorId);
Console.ReadLine();
it gives error for line
strProcessorId = info["processorId"].ToString();
error is: Object reference not set to an instance of an object.
how to remove this error
回答1:
WMI property names are probably case-sensitive. Try:
strProcessorId = info["ProcessorId"].ToString();
It might also help to properly capitalize the name of the Win32_Processor class:
SelectQuery query = new SelectQuery("Win32_Processor");
回答2:
try
string strProcessorId = string.Empty;
SelectQuery query = new SelectQuery("Win32_processor");
ManagementObjectSearcher search = new ManagementObjectSearcher(query);
foreach (ManagementObject info in search.Get())
{
strProcessorId = info["ProcessorID"].ToString();
}
Console.WriteLine(strProcessorId);
Console.ReadLine();
think it was just the capital missing that meant a null was being returned
来源:https://stackoverflow.com/questions/6679208/how-to-get-processor-id-from-win32-processor