Reputation: 11
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
Upvotes: 1
Views: 3423
Reputation: 4187
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
Upvotes: 1
Reputation: 263107
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");
Upvotes: 1