首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从WinXP或更高版本获取内存总量

从WinXP或更高版本获取内存总量
EN

Stack Overflow用户
提问于 2014-07-28 15:11:34
回答 1查看 143关注 0票数 0

关于这个话题已经有很多答案了,但是有没有一种方法可以从XP和更高版本(包括windows Server 2003 )获得Windows系统的内存总量?

我发现:

Win32_LogicalMemoryConfiguration (已弃用)

Win32_ComputerSystem (支持的最低客户端: Vista)

Microsoft.VisualBasic.Devices.ComputerInfo (根据平台不支持XP )

thx

EN

回答 1

Stack Overflow用户

发布于 2014-07-28 15:16:16

添加对Microsoft.VisualBasic的引用和

代码语言:javascript
复制
  var info = new Microsoft.VisualBasic.Devices.ComputerInfo();
  Debug.WriteLine(info.TotalPhysicalMemory);
  Debug.WriteLine(info.AvailablePhysicalMemory);
  Debug.WriteLine(info.TotalVirtualMemory);
  Debug.WriteLine(info.AvailableVirtualMemory);

编辑:How can I get the total physical memory in C#?

您可以使用GlobalMemoryStatusExExample Here

代码语言:javascript
复制
private void DisplayMemory()
{
    // Consumer of the NativeMethods class shown below
    long tm = System.GC.GetTotalMemory(true);
    NativeMethods oMemoryInfo = new NativeMethods();
    this.lblMemoryLoadNumber.Text = oMemoryInfo.MemoryLoad.ToString();
    this.lblIsMemoryTight.Text = oMemoryInfo.isMemoryTight().ToString();
    if (oMemoryInfo.isMemoryTight())
        this.lblIsMemoryTight.Text.Font.Bold = true;
    else
        this.lblIsMemoryTight.Text.Font.Bold = false;

}

本机类包装器。

代码语言:javascript
复制
[CLSCompliant(false)]
public class NativeMethods {

     private     MEMORYSTATUSEX msex;
     private uint _MemoryLoad;
     const int MEMORY_TIGHT_CONST = 80;

     public bool isMemoryTight()
      {
         if (_MemoryLoad > MEMORY_TIGHT_CONST )
            return true;
          else
             return false;
     }

          public uint MemoryLoad
     {
        get { return _MemoryLoad; }
         internal set { _MemoryLoad = value; }
      }

       public NativeMethods() {

        msex = new MEMORYSTATUSEX();
        if (GlobalMemoryStatusEx(msex)) {

                  _MemoryLoad = msex.dwMemoryLoad;
             //etc.. Repeat for other structure members        

        }
            else
             // Use a more appropriate Exception Type. 'Exception' should almost never be thrown
             throw new Exception("Unable to initalize the GlobalMemoryStatusEx API");
    }

    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    private class MEMORYSTATUSEX
    {
              public uint dwLength;
        public uint dwMemoryLoad;
              public ulong ullTotalPhys;
              public ulong ullAvailPhys;
              public ulong ullTotalPageFile;
              public ulong ullAvailPageFile;
              public ulong ullTotalVirtual;
              public ulong ullAvailVirtual;
              public ulong ullAvailExtendedVirtual;
              public MEMORYSTATUSEX()
        {
                  this.dwLength = (uint) Marshal.SizeOf(typeof( MEMORYSTATUSEX ));
        }
    }


     [return: MarshalAs(UnmanagedType.Bool)]
     [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
     static extern bool GlobalMemoryStatusEx( [In, Out] MEMORYSTATUSEX lpBuffer);

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24990125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档