Retrieving Total amount of RAM on a computer [duplicate]

≡放荡痞女 提交于 2019-11-29 13:39:23

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.

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