这可能不是最好的方法,但我目前正在使用以下方法检索机器上的RAM量:
manageObjSearch.Query = new ObjectQuery("SELECT TotalVisibleMemorySize FROM Win32_OperatingSystem");
manageObjCol = manageObjSearch.Get();
foreach (ManagementObject mo in manageObjCol)
sizeInKilobytes = Convert.ToInt64(mo["TotalVisibleMemorySize"]);
它效果很好,但是我觉得我可以更直接地做到这一点而且不需要预测单个元素,但我无法弄清楚如何索引ManagementObjectCollection
我希望做这样的事情:
ManagementObject mo = new ManagementObject("Win32_OperatingSystem.TotalVisibleMemorySize")
mo.Get();
Console.WriteLine(mo["TotalVisibleMemorySize"].ToString())
或者甚至可能是
ManagementClass mc = new ManagementClass("Win32_OperatingSystem");
Console.WriteLine(mc.GetPropertyValue("TotalVisibleMemorySize").ToString());
我似乎无法弄明白。有什么想法吗?
答案 0 :(得分:3)
foreach语句隐藏了您需要访问的枚举器。你可以直接这样做:
var enu = manageObjSearch.Get().GetEnumerator();
if (!enu.MoveNext()) throw new Exception("Unexpected WMI query failure");
long sizeInKilobytes = Convert.ToInt64(enu.Current["TotalVisibleMemorySize"]);