windows - 如何通过 WMI 查询获取以 GB 为单位的总物理内存 (ram) 信息?

标签 windows wmi wmic wql

我知道如何从 win32_computersystem 类中获取总物理内存。但它以字节或 kb 为单位。我想要以 MB 或 GB 为单位的此信息。在 wmi (wql) 查询中。 wmic 也可以。提前致谢。

最佳答案

您必须手动转换属性值。也更好用 Win32_PhysicalMemory WMI 类。

试试这个例子

using System;
using System.Collections.Generic;
using System.Management;
using System.Text;

namespace GetWMI_Info
{
    class Program
    {

        static void Main(string[] args)
        {
            try
            {
                ManagementScope Scope;
                Scope = new ManagementScope(String.Format("\\\\{0}\\root\\CIMV2", "."), null);

                Scope.Connect();
                ObjectQuery Query = new ObjectQuery("SELECT Capacity FROM Win32_PhysicalMemory");
                ManagementObjectSearcher Searcher = new ManagementObjectSearcher(Scope, Query);
                UInt64 Capacity = 0;
                foreach (ManagementObject WmiObject in Searcher.Get())
                {
                    Capacity+= (UInt64) WmiObject["Capacity"];
                }
                Console.WriteLine(String.Format("Physical Memory {0} gb", Capacity / (1024 * 1024 * 1024))); 
                Console.WriteLine(String.Format("Physical Memory {0} mb", Capacity / (1024 * 1024)));
            }
            catch (Exception e)
            {
                Console.WriteLine(String.Format("Exception {0} Trace {1}", e.Message, e.StackTrace));
            }
            Console.WriteLine("Press Enter to exit");
            Console.Read();
        }
    }
}

关于windows - 如何通过 WMI 查询获取以 GB 为单位的总物理内存 (ram) 信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16041249/

相关文章:

powershell - WMI 事件订阅和 PowerShell 执行

iis - 如何在 % { dir IIS :\AppPools\$_. Name } 中创建一个 For..Each 值?

linux - Linux 与 Windows 中的 WMIC 差异

batch-file - 结合 For/F 与 WMIC + WHERE 子句 + AND 子句

python - python 中的 wmic 调用返回无效查询错误

windows - 如何获取EFI分区的存储信息?

windows - WMIprvse 进程在 2008 服务器 R2 上泄漏内存

windows - 句柄分配在哪里?

windows - 检查进程是否使用批处理文件返回 0

windows - TControl.Perform 的替代方案