Skip to content

Instantly share code, notes, and snippets.

@sk8tz
Forked from wtarr/gist:5894837
Created August 31, 2016 17:32
Show Gist options
  • Save sk8tz/76bb6d3658bd2d8e9ef284a33bdfcdd6 to your computer and use it in GitHub Desktop.
Save sk8tz/76bb6d3658bd2d8e9ef284a33bdfcdd6 to your computer and use it in GitHub Desktop.
Find percentage used physical memory - with aid of WMI code generator http://www.microsoft.com/en-ie/download/details.aspx?id=8572
using System;
using System.Management;
namespace WMIQuery
{
public class UsedPhysicalMemoryQuery
{
public static void Main()
{
try
{
ManagementObjectSearcher searcher =
new ManagementObjectSearcher("root\\CIMV2",
"SELECT * FROM Win32_OperatingSystem");
foreach (ManagementObject queryObj in searcher.Get())
{
double free = Double.Parse(queryObj["FreePhysicalMemory"].ToString());
double total = Double.Parse(queryObj["TotalVisibleMemorySize"].ToString());
Console.WriteLine("Percentage used: {0}%", Math.Round(((total - free)/total * 100), 2));
Console.Read();
}
}
catch (ManagementException e)
{
Console.WriteLine(value: e.Message);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment