Solved

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

Posted on 2010-11-29
4
11,766 Views
Last Modified: 2012-05-10
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!
0
Comment
Question by:Enill
4 Comments
 
LVL 7

Expert Comment

by:luceysupport
ID: 34236860
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

0
 
LVL 3

Expert Comment

by:asurianu
ID: 34236878
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
0
 
LVL 2

Expert Comment

by:qasali
ID: 34238652
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

 
0
 
LVL 13

Accepted Solution

by:
Naman Goel earned 500 total points
ID: 34274430
You can use the PerformanceCounter class from System.Diagnostics:

PerformanceCounter cpuCounter;
PerformanceCounter ramCounter;

cpuCounter = new PerformanceCounter();

cpuCounter.CategoryName = "Processor";
cpuCounter.CounterName = "% Processor Time";
cpuCounter.InstanceName = "_Total";

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


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

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

for Disk Usage info :

using System;
using System.IO;

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);
        }
    }
}
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now