需要获取系统的总物理内存量及剩余内存量。
参考了sno888在2008-9-8回复“准确获取内存”的内容,编写的代码如下:
ManagementClass memo = new ManagementClass("Win32_OperatingSystem");
ManagementObjectCollection memoVol = memo.GetInstances();
double capacity = 0;
foreach (ManagementObject mo in memoVol)
{
capacity += ((Math.Round(Int64.Parse(mo.Properties["FreePhysicalMemory"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));
}
运行上述代码,得到的capacity值为0,是何原因?请教各位高手^^
参考了sno888在2008-9-8回复“准确获取内存”的内容,编写的代码如下:
ManagementClass memo = new ManagementClass("Win32_OperatingSystem");
ManagementObjectCollection memoVol = memo.GetInstances();
double capacity = 0;
foreach (ManagementObject mo in memoVol)
{
capacity += ((Math.Round(Int64.Parse(mo.Properties["FreePhysicalMemory"].Value.ToString()) / 1024 / 1024 / 1024.0, 1)));
}
运行上述代码,得到的capacity值为0,是何原因?请教各位高手^^
解决方案 »
- 请问ToList方法
- 我是初学者,该怎样理解代码及格式
- combox怎么让新添加的项目放在最上面
- VS2008 web页添加背景音乐~~~
- 求一个ASP。NET C# 写的下载原代码。 前台后台都有的。求一个能让新手明白的。
- 在c#中怎样在oracel数据库中建立一个表,实在不好意思只有10分了
- C# ASP.NET 中的日期时间型字段在工作中遇到的问题.!!!!! 求解!!!
- C#的源代码如何编译出在64位XP下运行的程序?
- 帅哥们进吧!小问题。
- C#中如何将类库文件中的数组数据赋值给界面上的label控件并显示
- C#怎样打开Word,Excel和ppt软件
- 该方法在C#或者VC中如何实现???
MEMORYSTATUS ms = new MEMORYSTATUS();
NativeMethods.GlobalMemoryStatus(out ms);
MessageBox.Show((ms.dwTotalPhys/1024/1024).ToString());[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct MEMORYSTATUS
{ /// DWORD->unsigned int
public uint dwLength; /// DWORD->unsigned int
public uint dwMemoryLoad; /// SIZE_T->ULONG_PTR->unsigned int
public uint dwTotalPhys; /// SIZE_T->ULONG_PTR->unsigned int
public uint dwAvailPhys; /// SIZE_T->ULONG_PTR->unsigned int
public uint dwTotalPageFile; /// SIZE_T->ULONG_PTR->unsigned int
public uint dwAvailPageFile; /// SIZE_T->ULONG_PTR->unsigned int
public uint dwTotalVirtual; /// SIZE_T->ULONG_PTR->unsigned int
public uint dwAvailVirtual;
} public partial class NativeMethods
{ /// Return Type: void
///lpBuffer: LPMEMORYSTATUS->_MEMORYSTATUS*
[System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint = "GlobalMemoryStatus")]
public static extern void GlobalMemoryStatus([System.Runtime.InteropServices.OutAttribute()] out MEMORYSTATUS lpBuffer); }