Get current CPU, RAM and Disk drive usage in C#
Asked Answered
G

4

30

How to get the CPU, RAM and Disk drive usage of the system in C# code?

Grissel answered 10/8, 2009 at 4:52 Comment(0)
I
27

Please search SO; there are several similiar questions:

Interrelate answered 10/8, 2009 at 4:54 Comment(2)
How to get the disk drive usage?Grissel
Through the Performance Counter class. By changing the category.Letterperfect
J
18

Here is a solution which will output disk usage, the total disk percent being used at the time that Timer99 is polled:

 using System;
 using System.Diagnostics;
 using System.Windows;

 namespace diskpercent
 {
     public partial class MainWindow : Window
     {
         DispatcherTimer Timer99 = new DispatcherTimer();
         public MainWindow()
         {
             InitializeComponent();
             Timer99.Tick += Timer99_Tick; // don't freeze the ui
             Timer99.Interval = new TimeSpan(0, 0, 0, 0, 1024);
             Timer99.IsEnabled = true;
         }
         public PerformanceCounter myCounter =
            new PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");
         public Int32 j = 0;
         public void Timer99_Tick(System.Object sender, System.EventArgs e)

         {
                 //Console.Clear();
             j = Convert.ToInt32(myCounter.NextValue());      
                 //Console.WriteLine(j);
             textblock1.Text = j.ToString();
         }
     }
 }

and here is a list of common performance counters:

 PerformanceCounter("Processor", "% Processor Time", "_Total");
 PerformanceCounter("Processor", "% Privileged Time", "_Total");
 PerformanceCounter("Processor", "% Interrupt Time", "_Total");
 PerformanceCounter("Processor", "% DPC Time", "_Total");
 PerformanceCounter("Memory", "Available MBytes", null);
 PerformanceCounter("Memory", "Committed Bytes", null);
 PerformanceCounter("Memory", "Commit Limit", null);
 PerformanceCounter("Memory", "% Committed Bytes In Use", null);
 PerformanceCounter("Memory", "Pool Paged Bytes", null);
 PerformanceCounter("Memory", "Pool Nonpaged Bytes", null);
 PerformanceCounter("Memory", "Cache Bytes", null);
 PerformanceCounter("Paging File", "% Usage", "_Total");
 PerformanceCounter("PhysicalDisk", "Avg. Disk Queue Length", "_Total");
 PerformanceCounter("PhysicalDisk", "Disk Read Bytes/sec", "_Total");
 PerformanceCounter("PhysicalDisk", "Disk Write Bytes/sec", "_Total");
 PerformanceCounter("PhysicalDisk", "Avg. Disk sec/Read", "_Total");
 PerformanceCounter("PhysicalDisk", "Avg. Disk sec/Write", "_Total");
 PerformanceCounter("PhysicalDisk", "% Disk Time", "_Total");
 PerformanceCounter("Process", "Handle Count", "_Total");
 PerformanceCounter("Process", "Thread Count", "_Total");
 PerformanceCounter("System", "Context Switches/sec", null);
 PerformanceCounter("System", "System Calls/sec", null);
 PerformanceCounter("System", "Processor Queue Length", null);
Joeyjoffre answered 25/1, 2017 at 0:37 Comment(1)
This one gives the free space on a disk : PerformanceCounter("LogicalDisk", "FreeMegabytes", "C:")Bumble
P
1

Drives

static void Main(string[] args)
{
var drives = DriveInfo.GetDrives();
foreach (DriveInfo info in drives)
{
      Console.WriteLine("Name: {0}\nSize: {1}\nDrive Format: {2}", info.Name, info.TotalSize, info.DriveFormat);
}
 Console.ReadLine();
}
Pseudocarp answered 26/6, 2018 at 14:18 Comment(0)
A
0

There is a NuGet package called hardware.Info by Jinjinov See https://github.com/Jinjinov/Hardware.Info for details.

Code sample:

hardwareInfo.RefreshAll();
//Memory
Console.WriteLine(hardwareInfo.MemoryStatus);

//CPU
foreach (var cpu in hardwareInfo.CpuList)
{
    Console.WriteLine(cpu);

    foreach (var cpuCore in cpu.CpuCoreList)
        Console.WriteLine(cpuCore);
}

//Drives
foreach (var drive in hardwareInfo.DriveList)
{
    Console.WriteLine(drive);

    foreach (var partition in drive.PartitionList)
    {
        Console.WriteLine(partition);

        foreach (var volume in partition.VolumeList)
            Console.WriteLine(volume);
    }
}
Annabellannabella answered 31/8, 2023 at 13:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.