Solved

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

Posted on 2010-11-29
4
12,245 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

910 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

24 Experts available now in Live!

Get 1:1 Help Now