Using ManagementObject to retrieve a single WMI property

Posted by Jesse on Stack Overflow See other posts from Stack Overflow or by Jesse
Published on 2010-04-27T20:42:18Z Indexed on 2010/04/27 20:43 UTC
Read the original article Hit count: 340

Filed under:
|
|

This probably isn't the best way, but I am currently retrieving the amount of RAM on a machine using:

manageObjSearch.Query = new ObjectQuery("SELECT TotalVisibleMemorySize FROM Win32_OperatingSystem");
manageObjCol = manageObjSearch.Get();

foreach (ManagementObject mo in manageObjCol)
 sizeInKilobytes = Convert.ToInt64(mo["TotalVisibleMemorySize"]);

It works well and good, but I feel I could be doing this more directly and without a foreach over a single element, but I can't figure out how to index a ManagementObjectCollection

I want to do something like this:

ManagementObject mo = new ManagementObject("Win32_OperatingSystem.TotalVisibleMemorySize")
mo.Get();

Console.WriteLine(mo["TotalVisibleMemorySize"].ToString())

or maybe even something like

ManagementClass mc = new ManagementClass("Win32_OperatingSystem");
Console.WriteLine(mc.GetPropertyValue("TotalVisibleMemorySize").ToString());

I just can't seem to figure it out. Any ideas?

© Stack Overflow or respective owner

Related posts about wmi

Related posts about win32