How to check item in collection in WMI ManagementObject

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

Im using WMI to get CPU information through Win32_Processor.
It returns a collection but some items are "not valid".

.....
foreach( ManagementObject oReturn in oReturnCollection )
{
Console.WriteLine(oReturn["Manufacturer"].ToString());
Console.WriteLine(oReturn["ProcessorId"].ToString());
}

If oReturn["ProcessorId"] does not "exist" it crash with error:
Additional information: Object reference not set to an instance of an object.

How can I test to see if oReturn["ProcessorId"] is set to an instance
befor I WriteLine(...) it?

Thanks,

Joao Rego
 
Hi,

I had the same problem, use

if (oReturn["ProcessorId"] != null)
<read operations here>

Cheers
 
Joao,

what if you first read the property in a object variable and check this
variable for *null* before printing it out?
 
OK... that's it... thanks for your help.



Salvador said:
Hi,

I had the same problem, use

if (oReturn["ProcessorId"] != null)
<read operations here>

Cheers

--
Salvador Alvarez Patuel
Exony Ltd - London, UK


Joao Rego said:
Hello,

Im using WMI to get CPU information through Win32_Processor.
It returns a collection but some items are "not valid".

....
foreach( ManagementObject oReturn in oReturnCollection )
{
Console.WriteLine(oReturn["Manufacturer"].ToString());
Console.WriteLine(oReturn["ProcessorId"].ToString());
}

If oReturn["ProcessorId"] does not "exist" it crash with error:
Additional information: Object reference not set to an instance of an object.

How can I test to see if oReturn["ProcessorId"] is set to an instance
befor I WriteLine(...) it?

Thanks,

Joao Rego
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top