Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving Total amount of RAM on a computer [duplicate]

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.

like image 579
D. Rattansingh Avatar asked Dec 11 '11 07:12

D. Rattansingh


People also ask

How do you calculate total RAM?

Here's how: Press Ctrl + Shift + Esc to launch Task Manager. Or, right-click the Taskbar and select Task Manager. Select the Performance tab to see current RAM usage displayed in the Memory box, and total RAM capacity listed under Physical Memory.

What is the total amount of RAM is insert in computer?

Press the Windows key , type Properties, and then press Enter . In the System Properties window, the Installed memory (RAM) entry displays the total amount of RAM installed in the computer. For example, in the picture below, there is 4 GB of memory installed in the computer.


2 Answers

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.

like image 72
Hans Passant Avatar answered Oct 17 '22 04:10

Hans Passant


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);
  }
}
like image 30
Glory Raj Avatar answered Oct 17 '22 04:10

Glory Raj