How to get Processor Id from win32 processor

扶醉桌前 提交于 2019-12-25 03:38:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!