zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    [Re] SpringMVC-5(Converter+拦截器+国际化+异常处理)
    [Re] SpringMVC-4(数据绑定+数据格式化+数据校验)
    [Re] SpringMVC-3(视图解析+RESTful CRUD)
    [Re] SpringMVC-2(数据输出+源码流程)
    常用SQL优化
    JAVA 中的标准注解
    linux第一发
    easyui js取消选中 Tree 指定节点
    axis1.4 服务端获取请求报文报文和客户端获取请求报文和响应报文
    对于两个实体类属性值的合并,java实现
  • 原文地址:https://www.cnblogs.com/nanfei/p/13162166.html
Copyright © 2011-2022 走看看