tags:

views:

16

answers:

2

I'm trying to access ManagementObjects in ManagementObjectCollection without using a foreach statement, maybe I'm missing something but I can't figure out how to do it, I need to do something like the following:

ManagementObjectSearcher query = new ManagementObjectSearcher(
     "select Name, CurrentClockSpeed from Win32_Processor");

ManagementObjectCollection queryCollection = query.Get();

ManagementObject mo = queryCollection[0];
A: 

ManagementObjectCollection implements IEnumerable or ICollection, so either you must iterate it via IEnumerable (ie foreach) or CopyTo an array via ICollection.

However since it supports IEnumerable you can use Linq :

ManagementObject mo = queryCollection.FirstOrDefault();
Preet Sangha
A: 

You're probably missing the cast:

ManagementObject mo = (ManagementObject)queryCollection[0];

...as I don't think ManagementObjectCollection is generic (and as such does not have a typed indexer.)

x0n