使用 ManagementObject 检索单个 WMI 属性

发布于 2024-08-30 16:27:03 字数 851 浏览 1 评论 0原文

这可能不是最好的方法,但我目前正在使用以下方法检索机器上的 RAM 量:

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

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

它工作得很好,但我觉得我可以更直接地执行此操作,并且无需对单个元素进行 foreach,但我无法弄清楚如何索引 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());

我似乎无法弄清楚的事情。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淡淡的优雅 2024-09-06 16:27:03

foreach 语句隐藏了您需要访问的枚举器。你可以直接这样做:

        var enu = manageObjSearch.Get().GetEnumerator();
        if (!enu.MoveNext()) throw new Exception("Unexpected WMI query failure");
        long sizeInKilobytes = Convert.ToInt64(enu.Current["TotalVisibleMemorySize"]);

The foreach statement is hiding the enumerator you need to access. You can do it directly like this:

        var enu = manageObjSearch.Get().GetEnumerator();
        if (!enu.MoveNext()) throw new Exception("Unexpected WMI query failure");
        long sizeInKilobytes = Convert.ToInt64(enu.Current["TotalVisibleMemorySize"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文