当前位置:编程学习 > C#/ASP.NET >>

求助:使用C#获取物理内存量

 需要获取系统的总物理内存量及剩余内存量。
 参考了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,是何原因?请教各位高手^^
--------------------编程问答-------------------- 试试这个http://www.soaspx.com/dotnet/csharp/csharp_20111125_8322.html --------------------编程问答-------------------- GlobalMemoryStatus

试试整个 --------------------编程问答--------------------

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);

    }
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,