11

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.

Community
  • 1
  • 1
D. Rattansingh
  • 1,569
  • 3
  • 19
  • 30
  • Check [this][1] . [1]: http://stackoverflow.com/questions/105031/c-sharp-how-do-you-get-total-amount-of-ram-the-computer-has – Shai Dec 11 '11 at 07:27
  • This have been answered here: http://stackoverflow.com/questions/105031/c-sharp-how-do-you-get-total-amount-of-ram-the-computer-has – Softnux Dec 11 '11 at 07:27
  • Yes it has but not by using the performance counter class. I'm trying to tackle this from the performance counter class directly and not via visual basic DLL. – D. Rattansingh Dec 11 '11 at 07:41

2 Answers2

17

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.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Could you advise me on how to set my topic as answered/completed? – D. Rattansingh Dec 11 '11 at 15:59
  • 3
    The question asks for amount of RAM on a machine, so this should be `new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory` rather than AvailablePhysicalMemory which displays amount of bytes currently allocated. – zgerd Jun 24 '14 at 18:54
  • Does not work in .NET 3.0+. [The entire `Devices` namespace has been removed](https://learn.microsoft.com/en-us/dotnet/core/compatibility/visualbasic#types-in-microsoftvisualbasicdevices-namespace-not-available). – julealgon Jun 14 '21 at 15:00
  • Target .NET Core 5 instead. – Hans Passant Jun 14 '21 at 15:13
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);
  }
}
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • For some reason this is retrieving the wrong value, suppose to be 3.34 g, it's retrieving 4 gb. I have some code to do this and was using it in an application of mines on my laptop for 6 years and working fine. Added some more RAM to my computer and suddenly doesn't work again. Thanks anyway. – D. Rattansingh Dec 11 '11 at 15:56
  • 6
    This is not retrieving the currently installed memory this is retrieving the max supported amount of memory that can be installed to the mainboard. –  May 06 '13 at 13:36