Ramachandran
Ramachandran

Reputation: 103

How I can get the installed RAM and CPU details c# .Net Core?

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

Upvotes: 1

Views: 2466

Answers (1)

Kevin Brydon
Kevin Brydon

Reputation: 13102

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.

Upvotes: 3

Related Questions