查询Windows系统、进程的CPU使用率、内存使用率

发布时间 2023-06-08 11:04:30作者: 94cool
using System.Diagnostics;
using System.Runtime.Versioning;

namespace ConsoleApp1
{
    class Program
    {
        [SupportedOSPlatform("windows")]
        static void Main(string[] args)
        {
            Performance performance = new Performance();

            //Sleep的时间间隔
            int interval = 10000;


            while (true)
            {
                Console.WriteLine(performance.ProcessPerformance("ConsoleApp1"));
                Console.WriteLine(performance.SystemPerformance());
                Console.WriteLine();
                Thread.Sleep(interval);
            }

            Console.ReadLine();
        }
    }

    public class Performance
    {
        bool isWindows = OperatingSystem.IsWindows();

        [SupportedOSPlatform("windows")]
        PerformanceCounter totalCpu = new PerformanceCounter("Processor", "% Processor Time", "_Total");
        [SupportedOSPlatform("windows")]
        PerformanceCounter totalMemory = new PerformanceCounter("Process", "Working Set", "_Total");


        /// <summary>
        /// 进程Cpu使用率、内存使用率
        /// </summary>
        /// <param name="processName"></param>
        /// <returns></returns>
        [SupportedOSPlatform("windows")]
        public PerformanceInfo ProcessPerformance(string processName)
        {
            PerformanceInfo performanceInfo = new PerformanceInfo();
            PerformanceCounter curMemory = new PerformanceCounter("Process", "Working Set", processName);
            PerformanceCounter curCpu = new PerformanceCounter("Process", "% Processor Time", processName);
            performanceInfo.CpuUsage = Math.Round(curCpu.NextValue() / Environment.ProcessorCount, 2);
            performanceInfo.MemoryUsage = Math.Round(curMemory.NextValue() / 1024, 2);

            return performanceInfo;
        }

        /// <summary>
        /// 系统cpu使用率
        /// </summary>
        /// <returns></returns>
        [SupportedOSPlatform("windows")]
        public PerformanceInfo SystemPerformance()
        {
            PerformanceInfo performanceInfo = new PerformanceInfo();
            if (isWindows)
            {
                performanceInfo.CpuUsage = Math.Round(totalCpu.NextValue(), 2);
                performanceInfo.MemoryUsage = Math.Round(totalMemory.NextValue() / 1024, 2);
            }
            return performanceInfo;
        }
    }
    /// <summary>
    /// 硬件资源使用率
    /// </summary>
    public class PerformanceInfo
    {
        /// <summary>
        /// CPU使用率
        /// </summary>
        public double CpuUsage { get; set; }
        /// <summary>
        /// 内存使用率,单位:KB
        /// </summary>
        public double MemoryUsage { get; set; }
        /// <summary>
        /// 磁盘空间使用率
        /// </summary>
        public double DiskUsage { get; set; }

        public override string ToString()
        {
            return $"CPU使用率:{CpuUsage}%\t内存使用率:{MemoryUsage},空间使用率:{DiskUsage}";
        }
    }
}