主机性能监控系统--3.获取内存性能信息

      事先说明一下,如果有看过第一篇的,这篇基本上可以不用看了,因为这篇95%的内容是从第一篇抠下来的,单独写这篇文章的目的是为了让这个系列的文章更完整一点,还请各位看官不要扔砖头啊。。哈哈

开工~~~~

一样的,效果图先上,要获取的信息如图所示:总的物理内存,可用物理内存,总的虚拟内存,可用虚拟内存,页面文件空间,可用页面文件空间

 

      同样的,需要一个类,命名为:MemoryInfo,类图如下:

     

ContractedBlock.gif ExpandedBlockStart.gif MemoryInfo
    public class MemoryInfo
ExpandedBlockStart.gifContractedBlock.gif    
{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
总的物理内存#region 总的物理内存
        
string strTotalVisibleMemorySize = string.Empty;  //总的物理内存

        
public string TotalVisibleMemorySize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strTotalVisibleMemorySize; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strTotalVisibleMemorySize = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
可用物理内存#region 可用物理内存
        
string strFreePhysicalMemory = string.Empty;  //可用物理内存

        
public string FreePhysicalMemory
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strFreePhysicalMemory; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strFreePhysicalMemory = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
总的虚拟内存#region 总的虚拟内存
        
string strTotalVirtualMemorySize = string.Empty;  //总的虚拟内存

        
public string TotalVirtualMemorySize
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strTotalVirtualMemorySize; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strTotalVirtualMemorySize = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
可用虚拟内存#region 可用虚拟内存
        
string strFreeVirtualMemory = string.Empty;  //可用虚拟内存

        
public string FreeVirtualMemory
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strFreeVirtualMemory; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strFreeVirtualMemory = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
页面文件大小#region 页面文件大小
        
string strSizeStoredInPagingFiles = string.Empty;  //页面文件大小

        
public string SizeStoredInPagingFiles
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strSizeStoredInPagingFiles; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strSizeStoredInPagingFiles = value; }
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
可用页面文件大小#region 可用页面文件大小
        
string strFreeSpaceInPagingFiles = string.Empty;

        
public string FreeSpaceInPagingFiles
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get return strFreeSpaceInPagingFiles; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set { strFreeSpaceInPagingFiles = value; }
        }

        
#endregion


        
public void GetMemoryInfo()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
//实例化一个ManagementClass类,并将Win32_OperatingSystem作为参数传递进去,
            
//这样就可以查询Win32_OperatingSystem这个类里面的一些信息了。
            ManagementClass mClass = new ManagementClass("Win32_OperatingSystem");

            
//获取Win32_OperatingSystem这个类的所有实例
            ManagementObjectCollection moCollection = mClass.GetInstances();

            
//对Win32_OperatingSystem这个类进行遍历
            foreach (ManagementObject mObject in moCollection)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                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,代码如下:

 

ContractedBlock.gif ExpandedBlockStart.gif MemoryInfo Form
    public partial class MemoryInfoForm : Form
ExpandedBlockStart.gifContractedBlock.gif    
{
        
public MemoryInfoForm()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            InitializeComponent();
        }


        ComputerInfo.MemoryInfo memoryInfo 
= new ComputerInfo.MemoryInfo();
        
private void MemoryInfoForm_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            memoryInfo.GetMemoryInfo();

ContractedSubBlock.gifExpandedSubBlockStart.gif            
定义一些需要用到的项#region 定义一些需要用到的项
ExpandedSubBlockStart.gifContractedSubBlock.gif            ListViewItem lviTotalVisibleMemorySize 
= new ListViewItem(new string[] "总的物理内存", memoryInfo.TotalVisibleMemorySize });
ExpandedSubBlockStart.gifContractedSubBlock.gif            ListViewItem lviFreePhysicalMemory 
= new ListViewItem(new string[] "可用物理内存", memoryInfo.FreePhysicalMemory });
ExpandedSubBlockStart.gifContractedSubBlock.gif            ListViewItem lviTotalVirtualMemorySize 
= new ListViewItem(new string[] "总的虚拟内存", memoryInfo.TotalVirtualMemorySize });
ExpandedSubBlockStart.gifContractedSubBlock.gif            ListViewItem lviFreeVirtualMemory 
= new ListViewItem(new string[] "可用虚拟内存", memoryInfo.FreeVirtualMemory });
ExpandedSubBlockStart.gifContractedSubBlock.gif            ListViewItem lviSizeStoredInPagingFiles 
= new ListViewItem(new string[] "页面文件空间", memoryInfo.SizeStoredInPagingFiles });
ExpandedSubBlockStart.gifContractedSubBlock.gif            ListViewItem lviFreeSpaceInPagingFiles 
= new ListViewItem(new string[] "可用页面文件空间", memoryInfo.FreeSpaceInPagingFiles });
            
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif            
将项添加到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


ContractedSubBlock.gifExpandedSubBlockStart.gif            
设置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)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            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多,如图所示,

      然而在“开始--程序--附件--系统工具--系统信息”里面显示的数据跟我获得的数据时一样的,所以对于这个数据我有点困惑,不知道哪位朋友了解其中的玄机呢,恳请告诉我下,谢谢。

      未完,待续~~~~

转载于:https://www.cnblogs.com/lxcsmallcity/archive/2009/11/16/1603720.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值