Solved

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

Posted on 2010-11-29
4
13,734 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 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