0

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!

Community
  • 1
  • 1
Kristian
  • 125
  • 2
  • 17
  • Keep in mind the value is in *bytes*, and larger units of measurement (KB, MB, GB, etc) aren't powers of 10. So take that value, divide it by 1024 to convert it to MB, and that value yet again by 1024 to convert it to GB. The result will be about 7.9GB. – easuter Dec 09 '16 at 14:33
  • 8 * 1024 * 1024 * 1024 = 8589934592. Then you'll lose some for raisins (hence why Windows will often differentiate between what's installed and what's usable) – Damien_The_Unbeliever Dec 09 '16 at 14:33
  • See this post: http://stackoverflow.com/questions/1553336/how-can-i-get-the-total-physical-memory-in-c, for converting the resulting value of 8482025472 to GB – Xeun Dec 09 '16 at 14:33

3 Answers3

1

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));
acidbabies
  • 97
  • 7
  • That doesn't come out to 8. – ChrisF Dec 09 '16 at 14:36
  • 1
    It will never be precisely 8. Also, if you want a floating point representation of the value, don't use `Convert.Int32`. Just calculate it with: `((float)InstalledRam)/(1024.0*1024.0*1024.0)` – easuter Dec 09 '16 at 14:38
0

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

Kristian
  • 125
  • 2
  • 17
0

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
deveton
  • 291
  • 4
  • 26