【问题标题】:Get total installed RAM in C# on Windows 10在 Windows 10 上使用 C# 获取总安装 RAM
【发布时间】:2017-04-25 01:25:35
【问题描述】:

我正在制作一个显示一些硬件信息的软件, 以及其他信息。

我的问题是:

我使用这段代码,方法在另一个线程中找到:https://stackoverflow.com/a/15790751/5782981

    public ulong InstalledRam { get; set; }

    InstalledRam = GetTotalMemoryInBytes();

    }

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

返回

8482025472

为了测试它我去

MessageBox.Show(InstalledRam.ToString());

我已经看到了一些工作,也看到它在 fx 上不起作用。视窗 7。

我安装了 8 GB。

我想知道为什么返回值是84...

谢谢!

【问题讨论】:

  • 请记住,该值以 字节 为单位,更大的测量单位(KB、MB、GB 等)不是 10 的幂。所以取那个值,除以 1024 以将其转换为 MB,然后将该值再除以 1024 以将其转换为 GB。结果将是大约 7.9GB。
  • 8 * 1024 * 1024 * 1024 = 8589934592。然后你会因为葡萄干而损失一些(因此 Windows 经常会区分已安装和可用)
  • 查看这篇文章:stackoverflow.com/questions/1553336/…,用于将结果值 8482025472 转换为 GB

标签: c# memory ulong


【解决方案1】:

TotalPhysicalMemory 以字节表示。 如果您希望将内存转换为 GB,请使用此示例:

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

【讨论】:

  • 那不是8。
  • 它永远不会精确到 8。此外,如果您想要该值的浮点表示,请不要使用 Convert.Int32。只需计算:((float)InstalledRam)/(1024.0*1024.0*1024.0)
【解决方案2】:

我认为我必须进行某种计算@easuter。

这样做:

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

这给了我 8089,这是我可以使用的值。

谢谢

【讨论】:

    【解决方案3】:

    最好只加载一次计算机信息。现在在这里使用 Nuget 已经足够快了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;
                }
            }
        }
    

    然后结果将只在运行时加载一次。 打印 ListBox 中的所有信息:

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

    结果为:

    • Windows 7 Ultimate x64 Service Pack1
    • 联想电脑
    • 4 GB 内存
    • 联想 T430

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 2017-06-14
      • 2021-03-15
      • 2018-03-22
      相关资源
      最近更新 更多