Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get my machine CPU, RAM & Disk details.

Example:

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

What I have tried:

C#
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());
        }

Please Help This Method GetUsedCPU()
Posted
Updated 17-Jan-18 17:21pm
v3

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.
 
Share this answer
 
v2
Comments
[no name] 17-Jan-18 8:29am    
public static string GetUsedCPU()// This one not working It always gives 0%
{
PerformanceCounter CPUCounter;
CPUCounter = new PerformanceCounter();
CPUCounter.CategoryName = "Processor";
CPUCounter.CounterName = "% Processor Time";
CPUCounter.InstanceName = "_Total";
return String.Format("{0:0.00}", CPUCounter.NextValue());
}


This one not working please help me
[no name] 17-Jan-18 9:13am    
Please look below the solution posted by Jochen
[no name] 17-Jan-18 9:26am    
I believe if you try this it will often report 0, the reason being is that the Performance Counter object needs 2 values to give an accurate reading. This might lead you to think that inserting cpuCounter.NextValue() before the return line would fix the problem however this is not the case. These counters tend to only be updated about once or twice a second so calling it twice in succession would likely just return the same value.

Just write below two lines
CPUCounter.NextValue();
System.Threading.Thread.Sleep(1000);
above the below line.
return String.Format("{0:0.00}", CPUCounter.NextValue());
[no name] 17-Jan-18 23:19pm    
Awesome !!!!!

Thank You So Much, My Dear Friend.
Member 15459924 8-Dec-21 7:25am    
There is no PerformanceCounter in System.Diagnostics
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:
Quote:
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.
 
Share this answer
 
v2
C#
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());
        }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900