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

前端 未结 5 876
执笔经年
执笔经年 2020-12-15 23:56

I am using the GlobalMemoryStatusEx function to retrieve information about memory, but this function doesn\'t work correctly. It returns 0 for all properties. I

5条回答
  •  遥遥无期
    2020-12-16 00:49

    If c# you can:

    Reference the Microsoft.VisualBasic assembly. Then import Microsoft.VisualBasic.Devices namespace.
    And finally use ComputerInfo to get the total physical memory.

    int bytesPerMebibyte = (1 << 20);  // http://physics.nist.gov/cuu/Units/binary.html
    ComputerInfo myCompInfo = new ComputerInfo();
    string physicalMemory = "Physical Memory: "
        + (myCompInfo.TotalPhysicalMemory / bytesPerMebibyte) + " MB";
    

提交回复
热议问题