Link to home
Start Free TrialLog in
Avatar of Enill
Enill

asked on

C# get current CPU usage, Memory usage, Disk usage

Hi,
i am currently looking for a way to get the current CPU/Memory/Disk usage in a C# application.

Need to be compatible with Windows Vista and Windows 7.

Not sure if WMI is compatible with both, i used to have some problem getting null value where it is impossible to get nothing.

Thanks in advance!
Avatar of luceysupport
luceysupport
Flag of Ireland image

There is a performancecounter component you can add to your project which allows you to get these values.

System.Diagnostics.PerformanceCounter

Set the category name and counter name, use multiple objects to get all of the data you need

I hope this helps

Avatar of asurianu
asurianu

For CPU and Memory Usage you can see this code project. For memory u need to just add properties of memory in the Processes .
http://www.codeproject.com/KB/system/processescpuusage.aspx

For Disk Usage you can use DriveInfo Class
Here is the msdn link with an example
http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx
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);
        }
    }
}

I hope this helps

 
ASKER CERTIFIED SOLUTION
Avatar of Naman Goel
Naman Goel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial