如何从win32处理器获取处理器ID

时间:2011-07-13 12:54:21

标签: c# c#-3.0 wmi

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();

它为行

提供错误
strProcessorId = info["processorId"].ToString();

错误是:Object reference not set to an instance of an object.

如何删除此错误

2 个答案:

答案 0 :(得分:1)

WMI属性名称可能区分大小写。尝试:

strProcessorId = info["ProcessorId"].ToString();

它可能也有助于正确地大写Win32_Processor类的名称:

SelectQuery query = new SelectQuery("Win32_Processor");

答案 1 :(得分:1)

    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();

认为这只是缺少资本意味着返回null