C# get TotalVisibleMemorySize FreePhysicalMemory TotalVirtualMemorySize FreeVirtualMemory

1.Install System.Management in Nuget;

2

using System.Management; 

namespace ConsoleApp37
{
    internal class Program
    {
        static void Main(string[] args)
        {
            System.Timers.Timer tm = new System.Timers.Timer();
            tm.Interval = 10000;
            tm.Elapsed += Tm_Elapsed;
            tm.Enabled = true;
            tm.Start();
            Console.ReadLine();
        }

        static void Tm_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            ObjectQuery wql = new ObjectQuery("SELECT * FROM Win32_OperatingSystem");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(wql);
            ManagementObjectCollection results = searcher.Get();

            foreach (ManagementObject ram in results)
            {
                Console.WriteLine($"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}");
                Console.WriteLine($"{string.Format("Total Visible Memory: {0:0.00} GB", (Convert.ToDouble(ram["TotalVisibleMemorySize"])) / (1024 * 1024)).ToString()}");
                Console.WriteLine(string.Format("Free Physical Memory: {0:0.00} GB", (Convert.ToDouble(ram["FreePhysicalMemory"])) / (1024 * 1024)).ToString());
                Console.WriteLine(string.Format("Total Virtual Memory Size: {0:0.00} GB", (Convert.ToDouble(ram["TotalVirtualMemorySize"])) / (1024 * 1024)).ToString());
                Console.WriteLine(string.Format("Free Virtual Memory: {0:0.00} GB", (Convert.ToDouble(ram["FreeVirtualMemory"])) / (1024 * 1024)).ToString());
                Console.WriteLine("\n\n");
            }
        }
    }
}

 

 

 

posted @ 2024-06-11 19:41  FredGrit  阅读(8)  评论(0编辑  收藏  举报