System.Management, Management Object Searcher, and RAM

2.4k views Asked by At

I am brand new to coding and I'm having a go at trying to make a simple app that shows computer information in a multiline textbox, as you do.

So, I managed to get the basics by searching the registry for os name, version, etc and using system.environment to get current logged in user, architecture, etc and I used ManagementObjectSearcher to get the name of my cpu and also my total ram.

My problem is that I have 4 memory sticks installed so while the app technically gives me what I ask for by returning the total size of each memory stick, I actually just want one instance of 16GB instead of 4 instances of 4GB. This is what I used:

ManagementObjectSearcher search = new ManagementObjectSearcher("Select * From Win32_PhysicalMemory");

foreach (ManagementObject ram in search.Get())
            {
                txtSystemInfo.Text += "\r\nRAM: " + Convert.ToDouble(ram.GetPropertyValue("Capacity")) / 1073741824 + "GB";
        }

Output basically looks like this:

Username: xxxx
OS: Windows x.x (arch)
Version: x.x Build xxxx
CPU: Intel blah blah
RAM: 4GB
RAM: 4GB
RAM: 4GB
RAM: 4GB

Obviously I've just jumped in the deep end which is probably why I just can't get this one to click so any help is much appreciated.

1

There are 1 answers

0
Alex K. On BEST ANSWER

Simply increment a total in the loop, then format and display;

UInt64 total = 0;
foreach (ManagementObject ram in search.Get())
{
    total += (UInt64)ram.GetPropertyValue("Capacity");
}

txtSystemInfo.Text += "\r\nRAM: " + total / 1073741824 + "GB";