如何在 .NET Standard 2.0 中获取总系统 RAM?

How to get total system RAM in .NET Standard 2.0?

正如标题所说,有没有办法在 .NET Standard 2.0 中获取总(已安装)系统 RAM?我的所有搜索都只出现在 .NET Framework 解决方案中。

您可以将 System.Management 与 NET Standard 2.0 一起使用。希望对您有所帮助:

using System;
using System.Diagnostics;
using System.Management;

namespace RamSystem
{
    public class RamSystem
    {
        public static void GetRAM()
        {
            ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
            ManagementObjectCollection results = searcher.Get();

            double res;

            foreach (ManagementObject result in results)
            {
                res = Convert.ToDouble(result["TotalVisibleMemorySize"]);
                double fres = Math.Round((res / (1024 * 1024)), 2);
                Console.WriteLine("Total usable memory size: " + fres + "GB");
                Console.WriteLine("Total usable memory size: " + res + "KB");
            }

            Console.ReadLine();
        }
    }
}