Retrieving Total amount of RAM on a computer [duplicate]

风格不统一 提交于 2019-11-28 07:16:33

问题


Possible Duplicate:
C# - How do you get total amount of RAM the computer has?

The following would retrieve how much ram is available:

PerformanceCounter ramCounter;
ramCounter = new PerformanceCounter("Memory", "Available MBytes");
Console.WriteLine("Total RAM: " + ramCounter.NextValue().ToString() + " MB\n\n");

Of course we will have to use the System.Diagnostics; class.

Does performancecounter have any functionality for retrieving the amount of RAM of a particular machine? I'm not talking about the amount of ram used or unused. I'm talking about the amount of ram the machine has.


回答1:


This information is already available directly in the .NET framework, you might as well use it. Project + Add Reference, select Microsoft.VisualBasic.

using System;

class Program {
    static void Main(string[] args) {
        Console.WriteLine("You have {0} bytes of RAM",
            new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory);
        Console.ReadLine();
    }
}

And no, it doesn't turn your C# code into vb.net.




回答2:


you can try like this

Add a Reference to System.Management.

private static void DisplayTotalRam()
{
  string Query = "SELECT MaxCapacity FROM Win32_PhysicalMemoryArray";
  ManagementObjectSearcher searcher = new ManagementObjectSearcher(Query);
  foreach (ManagementObject WniPART in searcher.Get())
  {
    UInt32 SizeinKB = Convert.ToUInt32(WniPART.Properties["MaxCapacity"].Value);
    UInt32 SizeinMB = SizeinKB / 1024;
    UInt32 SizeinGB = SizeinMB / 1024;
    Console.WriteLine("Size in KB: {0}, Size in MB: {1}, Size in GB: {2}", SizeinKB, SizeinMB, SizeinGB);
  }
}


来源:https://stackoverflow.com/questions/8462539/retrieving-total-amount-of-ram-on-a-computer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!