下面的代码演示的是使用WMI对象可获取取物理内存和可用内存大小,在使用WMI对象前,先要添加对System.Management的引用,然后就可以调用WMI对象,代码如下:
//获取总物理内存大小
ManagementClass cimobject1 = new ManagementClass("Win32_PhysicalMemory");
ManagementObjectCollection moc1 = cimobject1.GetInstances();
foreach (ManagementObject mo1 in moc1) {
capacity += ((Math.Round(Int64.Parse(mo1.Properties["Capacity"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));
}
moc1.Dispose();
cimobject1.Dispose();
//获取内存可用大小
ManagementClass cimobject2 = new ManagementClass("Win32_PerfFormattedData_PerfOS_Memory");
ManagementObjectCollection moc2 = cimobject2.GetInstances();
foreach (ManagementObject mo2 in moc2) {
available += ((Math.Round(Int64.Parse(mo2.Properties["AvailableMBytes"].Value.ToString()) / 1024.0, 1)));
}
moc2.Dispose();
cimobject2.Dispose();
Console.WriteLine("MemoryCapacity=" + capacity.ToString() + "G");
Console.WriteLine("MemoryAvailable=" + available.ToString() + "G");
Console.WriteLine("MemoryUsed=" + ((capacity - available)).ToString() + "G," + (Math.Round((capacity - available) / capacity * 100,0)).ToString() + "%");