C# 获取计算机CPU使用率/内存使用率/磁盘使用率

 

CPU:

void Main()
{
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("select PercentProcessorTime from Win32_PerfFormattedData_PerfOS_Processor WHERE Name=\"_Total\"");
    var cpuItem = searcher.Get().Cast<ManagementObject>().Select(item => new { PercentProcessorTime = item["PercentProcessorTime"] }).First();
    if (cpuItem != null && cpuItem.PercentProcessorTime != null)
        Debug.Print("CPU:" + cpuItem.PercentProcessorTime.ToString());
}

 

Disk:

复制代码
public static Dictionary<string,int> GetDisksUsedRate()
{
    var result = new Dictionary<string,int>();
    var searcher = new ManagementObjectSearcher("Select Name,PercentDiskTime from Win32_PerfFormattedData_PerfDisk_PhysicalDisk WHERE Name != \"_Total\"");
    var diskItems = searcher.Get().Cast<ManagementObject>().ToList();
    foreach (var diskItem in diskItems)
        result.Add(diskItem["Name"].ToString(), Convert.ToInt32(diskItem["PercentDiskTime"]));
    return result;
}
复制代码

 

Memory:

复制代码
#region 获取内存使用率

        #region 可用内存

        /// <summary>
        ///     获取可用内存
        /// </summary>
        internal static long? GetMemoryAvailable()
        {
            const int MbDiv = 1024 * 1024;
            long availablebytes = 0;
            var managementClassOs = new ManagementClass("Win32_OperatingSystem");
            foreach (var managementBaseObject in managementClassOs.GetInstances())
                if (managementBaseObject["FreePhysicalMemory"] != null)
                    availablebytes = 1024 * long.Parse(managementBaseObject["FreePhysicalMemory"].ToString());
            return availablebytes / MbDiv;
        }

        #endregion

        internal static double? GetMemoryUsed()
        {
            float? PhysicalMemory = GetPhysicalMemory();
            float? MemoryAvailable = GetMemoryAvailable();
            double? MemoryUsed = (double?)(PhysicalMemory - MemoryAvailable);
            double currentMemoryUsed = (double)MemoryUsed ;
            return currentMemoryUsed ;
        }

        private static long? GetPhysicalMemory()
        {
            //获得物理内存
            const int MbDiv = 1024 * 1024;
            var managementClass = new ManagementClass("Win32_ComputerSystem");
            var managementObjectCollection = managementClass.GetInstances();
            foreach (var managementBaseObject in managementObjectCollection)
                if (managementBaseObject["TotalPhysicalMemory"] != null)
                    return long.Parse(managementBaseObject["TotalPhysicalMemory"].ToString()) / MbDiv;
            return null;
        }

        public static double? GetMemoryUsedRate()
        {
            float? PhysicalMemory = GetPhysicalMemory();
            float? MemoryAvailable = GetMemoryAvailable();
            double? MemoryUsedRate =(double?)(PhysicalMemory - MemoryAvailable)/ PhysicalMemory;
            return MemoryUsedRate.HasValue ? Convert.ToDouble(MemoryUsedRate * 100) : 0;
        }

        #endregion
复制代码

 

posted on   空明流光  阅读(4924)  评论(0编辑  收藏  举报

编辑推荐:
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示