事先说明一下,如果有看过第一篇的,这篇基本上可以不用看了,因为这篇95%的内容是从第一篇抠下来的,单独写这篇文章的目的是为了让这个系列的文章更完整一点,还请各位看官不要扔砖头啊。。哈哈
开工~~~~
一样的,效果图先上,要获取的信息如图所示:总的物理内存,可用物理内存,总的虚拟内存,可用虚拟内存,页面文件空间,可用页面文件空间
同样的,需要一个类,命名为:MemoryInfo,类图如下:
MemoryInfo
public class MemoryInfo
{
总的物理内存#region 总的物理内存
string strTotalVisibleMemorySize = string.Empty; //总的物理内存
public string TotalVisibleMemorySize
{
get
{ return strTotalVisibleMemorySize; }
set
{ strTotalVisibleMemorySize = value; }
}
#endregion
可用物理内存#region 可用物理内存
string strFreePhysicalMemory = string.Empty; //可用物理内存
public string FreePhysicalMemory
{
get
{ return strFreePhysicalMemory; }
set
{ strFreePhysicalMemory = value; }
}
#endregion
总的虚拟内存#region 总的虚拟内存
string strTotalVirtualMemorySize = string.Empty; //总的虚拟内存
public string TotalVirtualMemorySize
{
get
{ return strTotalVirtualMemorySize; }
set
{ strTotalVirtualMemorySize = value; }
}
#endregion
可用虚拟内存#region 可用虚拟内存
string strFreeVirtualMemory = string.Empty; //可用虚拟内存
public string FreeVirtualMemory
{
get
{ return strFreeVirtualMemory; }
set
{ strFreeVirtualMemory = value; }
}
#endregion
页面文件大小#region 页面文件大小
string strSizeStoredInPagingFiles = string.Empty; //页面文件大小
public string SizeStoredInPagingFiles
{
get
{ return strSizeStoredInPagingFiles; }
set
{ strSizeStoredInPagingFiles = value; }
}
#endregion
可用页面文件大小#region 可用页面文件大小
string strFreeSpaceInPagingFiles = string.Empty;
public string FreeSpaceInPagingFiles
{
get
{ return strFreeSpaceInPagingFiles; }
set
{ strFreeSpaceInPagingFiles = value; }
}
#endregion
public void GetMemoryInfo()
{
//实例化一个ManagementClass类,并将Win32_OperatingSystem作为参数传递进去,
//这样就可以查询Win32_OperatingSystem这个类里面的一些信息了。
ManagementClass mClass = new ManagementClass("Win32_OperatingSystem");
//获取Win32_OperatingSystem这个类的所有实例
ManagementObjectCollection moCollection = mClass.GetInstances();
//对Win32_OperatingSystem这个类进行遍历
foreach (ManagementObject mObject in moCollection)
{
TotalVisibleMemorySize = ((ulong)mObject["TotalVisibleMemorySize"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取总的物理内存
FreePhysicalMemory = ((ulong)mObject["FreePhysicalMemory"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取可用物理内存
TotalVirtualMemorySize = ((ulong)mObject["TotalVirtualMemorySize"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取总的虚拟内存
FreeVirtualMemory = ((ulong)mObject["FreeVirtualMemory"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取可用虚拟内存
SizeStoredInPagingFiles = ((ulong)mObject["SizeStoredInPagingFiles"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取页面文件大小
FreeSpaceInPagingFiles = ((ulong)mObject["FreeSpaceInPagingFiles"] / 1024.0 / 1024).ToString("#0.00") + "G"; //获取可用页面文件大小
}
}
}
然后在HostPerformance添加一个窗体,命名为:MemoryInfoForm,代码如下:
MemoryInfo Form public partial class MemoryInfoForm : Form
{
public MemoryInfoForm()
{
InitializeComponent();
}
ComputerInfo.MemoryInfo memoryInfo = new ComputerInfo.MemoryInfo();
private void MemoryInfoForm_Load(object sender, EventArgs e)
{
memoryInfo.GetMemoryInfo();
定义一些需要用到的项#region 定义一些需要用到的项
ListViewItem lviTotalVisibleMemorySize = new ListViewItem(new string[]
{ "总的物理内存", memoryInfo.TotalVisibleMemorySize });
ListViewItem lviFreePhysicalMemory = new ListViewItem(new string[]
{ "可用物理内存", memoryInfo.FreePhysicalMemory });
ListViewItem lviTotalVirtualMemorySize = new ListViewItem(new string[]
{ "总的虚拟内存", memoryInfo.TotalVirtualMemorySize });
ListViewItem lviFreeVirtualMemory = new ListViewItem(new string[]
{ "可用虚拟内存", memoryInfo.FreeVirtualMemory });
ListViewItem lviSizeStoredInPagingFiles = new ListViewItem(new string[]
{ "页面文件空间", memoryInfo.SizeStoredInPagingFiles });
ListViewItem lviFreeSpaceInPagingFiles = new ListViewItem(new string[]
{ "可用页面文件空间", memoryInfo.FreeSpaceInPagingFiles });
#endregion
将项添加到ListView控件里面#region 将项添加到ListView控件里面
lvMemory.Items.Add(lviTotalVisibleMemorySize);
lvMemory.Items.Add(lviFreePhysicalMemory);
lvMemory.Items.Add(lviTotalVirtualMemorySize);
lvMemory.Items.Add(lviFreeVirtualMemory);
lvMemory.Items.Add(lviSizeStoredInPagingFiles);
lvMemory.Items.Add(lviFreeSpaceInPagingFiles);
#endregion
设置ListView的样式以及列#region 设置ListView的样式以及列
lvMemory.View = View.Details;
lvMemory.Columns.Add("项", 150);
lvMemory.Columns.Add("值", 100);
lvMemory.FullRowSelect = true;
lvMemory.GridLines = true;
#endregion
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
lvMemory.Items[1].SubItems[1].Text =memoryInfo.FreePhysicalMemory;
lvMemory.Items[3].SubItems[1].Text = memoryInfo.FreeVirtualMemory;
lvMemory.Items[5].SubItems[1].Text = memoryInfo.SizeStoredInPagingFiles;
}
}
至此,内存信息的小程序就完成了,不过,在这过程中我发现了一个奇怪的东西,上面有获取到了一个“总的虚拟内存”这个东东,但是,我看那个数据好像不太正确,因为我系统设置的虚拟内存只有4G多,如图所示,