Amit Patel
Amit Patel

Reputation: 205

How do i get Physical memory of my computer using WMI query in C#?

Is there a way to get available memory using WMI query in C# code

Upvotes: 0

Views: 3596

Answers (2)

Thomas Levesque
Thomas Levesque

Reputation: 292425

Code generated with 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);
            }
        }
    }
}

Upvotes: 3

Related Questions