如何让我的机器CPU,RAM和&磁盘细节? [英] How to get my machine CPU, RAM & disk details?

查看:55
本文介绍了如何让我的机器CPU,RAM和&磁盘细节?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让我的机器CPU,RAM和磁盘详情。



例如:



CPU - 50%使用,

RAM - 80%使用,

DISK - 80%使用。



我尝试过:



How to get my machine CPU, RAM & Disk details.

Example:

CPU - 50% Used,
RAM - 80% Used,
DISK - 80% Used.

What I have tried:

public static string GetUsedDisk() // This one working fine
        {
            double TotalDiskSize = 0;
            double AvailableDiskSize = 0;
            double UsedDiskSize = 0;
            double UsedDiskSizePercent = 0;
            DriveInfo[] drives = DriveInfo.GetDrives();

            foreach (DriveInfo drive in drives)
            {
                if (drive.IsReady)
                {
                    TotalDiskSize += drive.TotalSize;
                    AvailableDiskSize += drive.AvailableFreeSpace;
                }
            }
            UsedDiskSize = TotalDiskSize - AvailableDiskSize;
            UsedDiskSizePercent = (UsedDiskSize / TotalDiskSize) * 100;
            return String.Format("{0:0.00}", UsedDiskSizePercent);
        }

 public static string GetUsedRAM()// This one working fine
        {
            PerformanceCounter RAMCounter;
            RAMCounter = new PerformanceCounter();
            RAMCounter.CategoryName = "Memory";
            RAMCounter.CounterName = "% Committed Bytes In Use";
            return String.Format("{0:0.00}", RAMCounter.NextValue());
        }

// This one not working It always gives 0% Please Help 
  public static string GetUsedCPU()        {
            PerformanceCounter CPUCounter;
            CPUCounter = new PerformanceCounter();
            CPUCounter.CategoryName = "Processor";
            CPUCounter.CounterName = "% Processor Time";
            CPUCounter.InstanceName = "_Total";
            return String.Format("{0:0.00}", CPUCounter.NextValue());
        }



请帮助此方法 GetUsedCPU()

推荐答案

For CPU Usage, use PerformanceCounter CLass from System.Diagnostics.

For example.

PerformanceCounter cpu;
PerformanceCounter ramr;

cpu = new PerformanceCounter();
cpu.CategoryName = "Processor";
cpu.CounterName = "% Processor Time";
cpu.InstanceName = "_Total";

ram = new PerformanceCounter("Memory", "Available MBytes");


public string getCurrentCpuUsage()
{
            cpu.NextValue()+"%";
}

public string getAvailableRAM()
{
            ramCounter.NextValue()+"MB";
}


For Disk usage here is a sample program.

Using System.IO;
using System;

class Info {
    public static void Main() {
        DriveInfo[] drives = DriveInfo.GetDrives();
        foreach (DriveInfo drive in drives) {
            //There are more attributes you can use.
            //Check the MSDN link for a complete example.
            Console.WriteLine(drive.Name);
            if (drive.IsReady) Console.WriteLine(drive.TotalSize);
        }
    }
}







此代码可能提供编译问题,所以不要直接复制粘贴。这是一种写在这里的方法。如果它回答了你的疑问,你可以投票支持这个解决方案。




This code may give a compilation issues,So don't copy paste directly.This is an approach written here.You can vote for this solution if it answers your doubt.


这是更新问题的一个问题CPU使用率始终为零。



许多 PerformanceCounter类(System.Diagnostics) [ ^ ]值取决于两次读取。这意味着在创建 PerformanceCounter 实例后, PerformanceCounter.NextValue Method(System.Diagnostics) [ ^ ]应在创建后调用一次,忽略返回的值。接下来的电话将返回通话之间的时间值。



所以你必须让你的 CPUCounter 一个类的静态成员(例如你的app类),初始化它,然后调用 NextValue()一次。然后进一步 NextValue()调用应返回非零。



另请参阅上述MSDN链接的示例代码和 NextValue()文档中的这个注释:

This is an anwser for the updated question where the CPU usage is always zero.

Many PerformanceCounter Class (System.Diagnostics)[^] values depend on two reads. That means after a PerformanceCounter instance has been created, PerformanceCounter.NextValue Method (System.Diagnostics)[^] should be called once after creation ignoring the returned value. Following calls will then return the value for the time between the calls.

So you have to make your CPUCounter a static member of a class (e.g. your app class), initialise it, and call NextValue() once. Then further NextValue() calls should return non zero.

See also the example code of the above MSDN links and this note in the NextValue() documentation:
引用:

如果计数器的计算值取决于两个计数器读数,则第一个读取操作返回0.0。重置性能计数器属性以指定其他计数器等效于创建新的性能计数器,使用新属性的第一个读取操作返回0.0。调用NextValue方法之间建议的延迟时间是一秒,以允许计数器执行下一次增量读取。

If the calculated value of a counter depends on two counter reads, the first read operation returns 0.0. Resetting the performance counter properties to specify a different counter is equivalent to creating a new performance counter, and the first read operation using the new properties returns 0.0. The recommended delay time between calls to the NextValue method is one second, to allow the counter to perform the next incremental read.


public static string GetUsedDisk() 
        {
            double TotalDiskSize = 0;
            double AvailableDiskSize = 0;
            double UsedDiskSize = 0;
            double UsedDiskSizePercent = 0;
            DriveInfo[] drives = DriveInfo.GetDrives();

            foreach (DriveInfo drive in drives)
            {
                if (drive.IsReady)
                {
                    TotalDiskSize += drive.TotalSize;
                    AvailableDiskSize += drive.AvailableFreeSpace;
                }
            }
            UsedDiskSize = TotalDiskSize - AvailableDiskSize;
            UsedDiskSizePercent = (UsedDiskSize / TotalDiskSize) * 100;
            return String.Format("{0:0.00}", UsedDiskSizePercent);
        }

 public static string GetUsedRAM()
        {
            PerformanceCounter RAMCounter;
            RAMCounter = new PerformanceCounter();
            RAMCounter.CategoryName = "Memory";
            RAMCounter.CounterName = "% Committed Bytes In Use";
            return String.Format("{0:0.00}", RAMCounter.NextValue());
        }


  public static string GetUsedCPU()        {
            PerformanceCounter CPUCounter;
            CPUCounter = new PerformanceCounter();
            CPUCounter.CategoryName = "Processor";
            CPUCounter.CounterName = "% Processor Time";
            CPUCounter.InstanceName = "_Total";
            CPUCounter.NextValue()
            System.Threading.Thread.Sleep(1000);
            return String.Format("{0:0.00}", CPUCounter.NextValue());
        }


这篇关于如何让我的机器CPU,RAM和&磁盘细节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆