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!
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!
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
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.Na me);
if (drive.IsReady) Console.WriteLine(drive.To talSize);
}
}
}
I hope this helps
For example.
PerformanceCounter cpu;
PerformanceCounter ramr;
cpu = new PerformanceCounter();
cpu.CategoryName = "Processor";
cpu.CounterName = "% Processor Time";
cpu.InstanceName = "_Total";
ram = new PerformanceCounter("Memory
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.Na
if (drive.IsReady) Console.WriteLine(drive.To
}
}
}
I hope this helps
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
System.Diagnostics.Perform
Set the category name and counter name, use multiple objects to get all of the data you need
I hope this helps