windows 如何通过 WMI 查询获取以 GB 为单位的总物理内存(ram)信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16041249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get total physical memory (ram) information in GB by WMI query?
提问by user2287201
I know how to get total physical memory from win32_computersystem class. but that comes in bytes or kb. I want this information in MB or GB. in wmi (wql) query. wmic also work. thanks in advance.
我知道如何从 win32_computersystem 类中获取总物理内存。但它以字节或 kb 为单位。我想要以 MB 或 GB 为单位的此信息。在 wmi (wql) 查询中。wmic 也工作。提前致谢。
回答by RRUZ
You must convert the value of the property manually. Also is better use Win32_PhysicalMemoryWMI class.
您必须手动转换属性的值。也最好使用Win32_PhysicalMemoryWMI 类。
Try this sample
试试这个样本
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();
}
}
}
回答by Arshad
you can convert TotalPhysicalMemory
of Win32_ComputerSystem. Try this :
你可以转换TotalPhysicalMemory
的的Win32_ComputerSystem。尝试这个 :
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)
{
}
}
}
}
回答by JFoushee
Wanted to make mention that I used the Win32_PhysicalMemory Capacity property until I met inconsistent results on Windows Server 2012. Now I use both properties (Win32_ComputerSystem:TotalPhysicalMemory and Win32_PhysicalMemory:Capacity) and choose the larger of the two.
想提一下,我使用了 Win32_PhysicalMemory 容量属性,直到我在 Windows Server 2012 上遇到不一致的结果。现在我使用这两个属性(Win32_ComputerSystem:TotalPhysicalMemory 和 Win32_PhysicalMemory:Capacity)并选择两者中较大的一个。