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

问题描述 投票:0回答:4

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

windows wmi wmic wql
4个回答
7
投票

您可以转换

Win32_ComputerSystem
TotalPhysicalMemory。试试这个:

using System;
using System.Management;
namespace WMISample
{
    public class MyWMIQuery
    {
        public static void Main()
        {
            try
            {
                ManagementObjectSearcher searcher =
                    new ManagementObjectSearcher("root\\CIMV2",
                    "SELECT TotalPhysicalMemory FROM Win32_ComputerSystem");

                foreach (ManagementObject queryObj in searcher.Get())
                { 
                    double dblMemory;
                    if(double.TryParse(Convert.ToString(queryObj["TotalPhysicalMemory"]),out dblMemory))
                    {
                        Console.WriteLine("TotalPhysicalMemory is: {0} MB", Convert.ToInt32(dblMemory/(1024*1024)));
                        Console.WriteLine("TotalPhysicalMemory is: {0} GB", Convert.ToInt32(dblMemory /(1024*1024*1024)));
                    }
                }
            }
            catch (ManagementException e)
            {

            }
        }
    }
}

6
投票

您必须手动转换属性的值。最好还是使用 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();
        }
    }
}

3
投票

想要提一下,我使用了 Win32_PhysicalMemory 容量属性,直到在 Windows Server 2012 上遇到不一致的结果。现在我使用这两个属性(Win32_ComputerSystem:TotalPhysicalMemory 和 Win32_PhysicalMemory:Capacity)并选择两者中较大的一个。


1
投票

对于现在登陆这里的任何人(这篇文章已有 10 年历史)

Get-WmiObjectPowerShell 3.0 及更高版本中已弃用。 (来源:https://ss64.com/ps/get-wmiobject.html

Get-WmiObject 正在替换为 Get-CimInstance,它在很多方面都好得多!

如果您仍然想这样做,请考虑使用以下 -

代码

$MemoryUncleaned = (Get-CimInstance Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).sum /1gb
$Memory = $MemoryUncleaned.ToString() + "GB"
$Memory

输出

64GB
© www.soinside.com 2019 - 2024. All rights reserved.