c# 物理内存大小 Windows可见内存大小 内存信息


转自http://blog.csdn.net/zxkid/archive/2006/11/21/1401421.aspx
十分感谢
zxkid 写的这篇文章。

记得要加入 System.Management命名空间,在解决方案中另击选择添加引用,在弹出的对话框中找到即可

using System;
None.gif
using System.Collections.Generic;
None.gif
using System.ComponentModel;
None.gif
using System.Management;
None.gifNone.gif
ContractedBlock.gifExpandedBlockStart.gif       
#region 得到Windows可用物理内存大小
InBlock.gif        
string GetPhysicalMemorySize()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
...{
InBlock.gif            
//PhysicalMemorySize 可用物理内存大小,与资源管理器中的关于对话框显示的内存大小一致
InBlock.gif            
//FreePhysicalMemory 剩余物理内存大小
InBlock.gif
            ulong PhysicalMemorySize = 0, VirtualMemorySize = 0, FreePhysicalMemory = 0;
ContractedSubBlock.gifExpandedSubBlockStart.gif            
调用方式一#region 调用方式一
InBlock.gif            
//ManagementObjectSearcher seacher = new ManagementObjectSearcher("Select * From Win32_OperatingSystem");
InBlock.gif            
//foreach (ManagementBaseObject obj in seacher.Get())
InBlock.gif            
//{
InBlock.gif            
//     if (obj["TotalVisibleMemorySize"] != null)
InBlock.gif            
//         PhysicalMemorySize = (ulong)obj["TotalVisibleMemorySize"];
InBlock.gif
InBlock.gif            
//     if (obj["TotalVirtualMemorySize"] != null)
InBlock.gif            
//         VirtualMemorySize = (ulong)obj["TotalVirtualMemorySize"];
InBlock.gif
InBlock.gif            
//     if (obj["FreePhysicalMemory"] != null)
InBlock.gif            
//         FreePhysicalMemory = (ulong)obj["FreePhysicalMemory"];
InBlock.gif            
//     break;
InBlock.gif            
//}
ExpandedSubBlockEnd.gif
            #endregion
ContractedSubBlock.gifExpandedSubBlockStart.gif            
调用方式二#region 调用方式二
InBlock.gif             ManagementClass osClass
= new ManagementClass("Win32_OperatingSystem");
InBlock.gif            
foreach (ManagementObject obj in osClass.GetInstances())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
...{
InBlock.gif                
if (obj["TotalVisibleMemorySize"] != null)
InBlock.gif                     PhysicalMemorySize
= (ulong)obj["TotalVisibleMemorySize"];
InBlock.gif
InBlock.gif                
if (obj["TotalVirtualMemorySize"] != null)
InBlock.gif                     VirtualMemorySize
= (ulong)obj["TotalVirtualMemorySize"];
InBlock.gif
InBlock.gif                
if (obj["FreePhysicalMemory"] != null)
InBlock.gif                     FreePhysicalMemory
= (ulong)obj["FreePhysicalMemory"];
InBlock.gif                
break;
ExpandedSubBlockEnd.gif             }

ExpandedSubBlockEnd.gif            
#endregion
InBlock.gif
InBlock.gif            
if (PhysicalMemorySize > 0)
InBlock.gif                
return string.Format("{0:###,###,###} KB", PhysicalMemorySize);
InBlock.gif            
else
InBlock.gif                
return "Unknown";
ExpandedSubBlockEnd.gif         }

ExpandedBlockEnd.gif        
#endregion
None.gif
得到Windows可用物理内存大小

得到物理内存的大小的代码如下:
ExpandedBlockStart.gif        #region 得到物理内存信息
InBlock.gif        
string GetPhysicalMemory()

转载于:https://www.cnblogs.com/xiaoxia/archive/2009/08/31/1557682.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值