如何在C#中使用WMI查询获取计算机的物理内存?

时间:2011-10-10 08:16:45

标签: c# asp.net wmi

有没有办法在C#代码中使用WMI查询获取可用内存

2 个答案:

答案 0 :(得分:3)

使用WMI Code Creator生成的代码:

using System;
using System.Management;
using System.Windows.Forms;

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

                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Win32_ComputerSystem instance");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("TotalPhysicalMemory: {0}", queryObj["TotalPhysicalMemory"]);
                }
            }
            catch (ManagementException e)
            {
                MessageBox.Show("An error occurred while querying for WMI data: " + e.Message);
            }
        }
    }
}

答案 1 :(得分:0)