本文介绍了如何通过WMI查询以GB为单位获取总物理内存(RAM)信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

,您可以转换TotalPhysicalMemory %28v = vs.85%29.aspx"rel =" noreferrer> Win32_ComputerSystem .试试这个:

you can convert TotalPhysicalMemory of Win32_ComputerSystem. Try this :

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)
            {

            }
        }
    }
}

这篇关于如何通过WMI查询以GB为单位获取总物理内存(RAM)信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 06:29