Kristian
Kristian

Reputation: 135

Get total installed RAM in C# on Windows 10

I am making a piece of software which displays some hardware information, along with other pieces of info.

My Problem is:

I use this piece of code, method found in another thread: https://stackoverflow.com/a/15790751/5782981

    public ulong InstalledRam { get; set; }

    InstalledRam = GetTotalMemoryInBytes();

    }

    static ulong GetTotalMemoryInBytes()
    {
        return new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory;
    }

This returns

8482025472

To test it I go

MessageBox.Show(InstalledRam.ToString());

I've seen this work for some, and also seen it not working on fx. Windows 7.

I have 8 GB installed.

I want to know why the return value is 84...

Thank you!

Upvotes: 0

Views: 7900

Answers (3)

deveton
deveton

Reputation: 341

Its better to only Load Computer info only once. Now with using Nuget here is fast enough https://www.nuget.org/packages/OSVersionInfo/

           public static class ComputerInformation
    {
        private static string _WindowsEdition;
        private static string _ComputerName;
        private static string _Processor;
        private static string _RAM;
        private static string _Model;

        private static void FillPCInfo()
        {
            ManagementObjectSearcher Search = new ManagementObjectSearcher();
            Search.Query = new ObjectQuery("Select * From Win32_ComputerSystem");
            foreach (ManagementObject obj in Search.Get())
            {
                _RAM = $"{Math.Round(Convert.ToDouble(obj["TotalPhysicalMemory"]) / (1024 * 1024 * 1024))} GB";
                _Model = obj["Model"]?.ToString();
                if (!string.IsNullOrWhiteSpace(_RAM))
                    break;
            }
        }

        public static string WindowsEdition
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_WindowsEdition))
                    return _WindowsEdition = $"{JCS.OSVersionInfo.Name} {JCS.OSVersionInfo.Edition} {(JCS.OSVersionInfo.OSBits == JCS.OSVersionInfo.SoftwareArchitecture.Bit32 ? "x86" : "x64")} {JCS.OSVersionInfo.ServicePack}".Trim();
                return _WindowsEdition;
            }
        }
        public static string ComputerName
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_ComputerName))
                    return _ComputerName = Environment.MachineName;
                return _ComputerName;
            }
        }

        public static string Processor
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_Processor))
                {
                    ManagementObjectSearcher Search = new ManagementObjectSearcher();
                    Search.Query = new ObjectQuery("SELECT * FROM Win32_Processor");
                    var SearchResult = Search.Get();
                    foreach (ManagementObject obj in SearchResult)
                    {
                        _Processor = $"{obj["Name"]} {(SearchResult.Count > 1 ? "(2 processors)" : string.Empty)}".Trim();
                        if (!string.IsNullOrWhiteSpace(Processor))
                            break;
                    }
                    return _Processor;
                }
                return _Processor;
            }
        }

        public static string RAM
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_RAM))
                {
                    FillPCInfo();
                    return _RAM;
                }
                return _RAM;
            }
        }

        public static string Model
        {
            get
            {
                if (string.IsNullOrWhiteSpace(_Model))
                {
                    FillPCInfo();
                    return _Model;
                }
                return _Model;
            }
        }
    }

Then result will be only load once at run-time. Print all info in ListBox:

        listBox1.Items.AddRange(new string[] {ComputerInformation.WindowsEdition, ComputerInformation.ComputerName, ComputerInformation.Processor, ComputerInformation.PC.RAM, ComputerInformation.PC.Model});

Which result as:

  • Windows 7 Ultimate x64 Service Pack1
  • Lenovo-PC
  • 4 GB Ram
  • Lenovo T430

Upvotes: 0

Kristian
Kristian

Reputation: 135

I thought about that I had to make some sort of calculation @easuter.

By doing so:

var ram = InstalledRam / 1024 / 1024;
MessageBox.Show(ram.ToString());

This gives me 8089 which is a value that I can work with.

Thanks

Upvotes: 0

acidbabies
acidbabies

Reputation: 107

The TotalPhysicalMemory is expressed in bytes. If you want the memory to be converted to GB use this sample:

Convert.ToInt32(InstalledRam/(1024*1024*1024));

Upvotes: 1

Related Questions