1

I need a installed total RAM in GB and also the CPU details as well. I see lot post related to this. But, I'm not sure which is suitable for me. And, also I tried with couple of examples but its not provided the expected result.

For example, I need like below,

RAM Size = 16 GB

CPU = Intel(R) Core(TM) i5 3.20 GHz 2 Cores

Note : I'm not expect the available or used memory.

Update :

To get the RAM details I used the below code

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

But, This gave me wrong Data(Infact, it give 32 GB instead of 16GB)

And, to get the CPU details

Ramachandran
  • 103
  • 12
  • 4
    "*but facing some challenges related to permission and environment*" try some. "*also I tried with couple of examples but its not provided the expected result*" show us what you have tried. We cant guess what you have done and what results you have seen. – TheGeneral Jun 22 '20 at 09:25
  • 1
    You are 100% sure you have 16GB and not 32GB? I ask because I helped a colleague write something similar and also reported the wrong number being returned until he actually checked his PC and found that when it was last serviced by central IT they had also installed more memory without telling him. – Lasse V. Karlsen Jun 22 '20 at 09:38
  • Ya, I'm sure Lasse. I'm not sure how to attach the image here. – Ramachandran Jun 22 '20 at 09:40
  • Ya... I have attached the image jps. Thanks – Ramachandran Jun 22 '20 at 09:44
  • 1
    Is MaxCapacity the maximum RAM that the system supports, rather than what's actually installed? Have you looked at other answers on SO that use Win32_PhysicalMemory instead? e.g. https://stackoverflow.com/questions/105031/how-do-you-get-total-amount-of-ram-the-computer-has – Ian Gilroy Jun 22 '20 at 09:53
  • 1
    Please stop voting to close the question. @Ramachandran has asked a specific question and has shown some effort in trying to get the correct answer. I've also learned something new today by investigating the problem. – Kevin Brydon Jun 22 '20 at 10:01

1 Answers1

2

Looks like you're using the wrong class. Try the Win32_PhysicalMemory class and the Capacity property

var query = "SELECT Capacity FROM Win32_PhysicalMemory";
var searcher = new ManagementObjectSearcher(query);
foreach (var WniPART in searcher.Get())
{
    var capacity = Convert.ToUInt64(WniPART.Properties["Capacity"].Value);
    var capacityKB = capacity / 1024;
    var capacityMB = capacityKB / 1024;
    var capacityGB = capacityMB / 1024;
    System.Console.WriteLine("Size in KB: {0}, Size in MB: {1}, Size in GB: {2}", capacityKB, capacityMB, capacityGB);
}

The MaxCapacity property in the Win32_PhysicalMemoryArray class will give you the maximum amount of RAM installable on your machine, not the amount of memory that is actually installed.

Kevin Brydon
  • 12,524
  • 8
  • 46
  • 76
  • Hi Kevin Brydon, Thanks for your solution its work for me. Any idea how to get the CPU name? – Ramachandran Jun 22 '20 at 10:30
  • @Ramachandran You'd have to find the relevant class and property from the documentation https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-provider – Kevin Brydon Jun 23 '20 at 03:25
  • Ya... Thanks Kevin. I found the solution for that issue. But, these solutions only work on windows environment. My application should run Windows, Linux and MacOS. So, while running my application with linux its wont give any result. – Ramachandran Jun 23 '20 at 04:47