转自http://blog.csdn.net/zxkid/archive/2006/11/21/1401421.aspx
十分感谢 zxkid 写的这篇文章。
记得要加入 System.Management命名空间,在解决方案中另击选择添加引用,在弹出的对话框中找到即可
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Management;
#region 得到Windows可用物理内存大小
string GetPhysicalMemorySize()
...{
//PhysicalMemorySize 可用物理内存大小,与资源管理器中的关于对话框显示的内存大小一致
//FreePhysicalMemory 剩余物理内存大小
ulong PhysicalMemorySize = 0, VirtualMemorySize = 0, FreePhysicalMemory = 0;
调用方式一#region 调用方式一
//ManagementObjectSearcher seacher = new ManagementObjectSearcher("Select * From Win32_OperatingSystem");
//foreach (ManagementBaseObject obj in seacher.Get())
//{
// if (obj["TotalVisibleMemorySize"] != null)
// PhysicalMemorySize = (ulong)obj["TotalVisibleMemorySize"];
// if (obj["TotalVirtualMemorySize"] != null)
// VirtualMemorySize = (ulong)obj["TotalVirtualMemorySize"];
// if (obj["FreePhysicalMemory"] != null)
// FreePhysicalMemory = (ulong)obj["FreePhysicalMemory"];
// break;
//}
#endregion
调用方式二#region 调用方式二
ManagementClass osClass = new ManagementClass("Win32_OperatingSystem");
foreach (ManagementObject obj in osClass.GetInstances())
...{
if (obj["TotalVisibleMemorySize"] != null)
PhysicalMemorySize = (ulong)obj["TotalVisibleMemorySize"];
if (obj["TotalVirtualMemorySize"] != null)
VirtualMemorySize = (ulong)obj["TotalVirtualMemorySize"];
if (obj["FreePhysicalMemory"] != null)
FreePhysicalMemory = (ulong)obj["FreePhysicalMemory"];
break;
}
#endregion
if (PhysicalMemorySize > 0)
return string.Format("{0:###,###,###} KB", PhysicalMemorySize);
else
return "Unknown";
}
#endregion
得到Windows可用物理内存大小
得到物理内存的大小的代码如下:
#region 得到物理内存信息
string GetPhysicalMemory()