在 C# 中获取当前 CPU、RAM 和磁盘驱动器的使用情况

Posted

技术标签:

【中文标题】在 C# 中获取当前 CPU、RAM 和磁盘驱动器的使用情况【英文标题】:Get current CPU, RAM and Disk drive usage in C# 【发布时间】:2010-11-18 04:44:53 【问题描述】:

如何在C#代码中获取系统的CPU、RAM和磁盘驱动器使用情况?

【问题讨论】:

【参考方案1】:

驱动器

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();

【讨论】:

【参考方案2】:

这是一个输出磁盘使用情况的解决方案,即在轮询 Timer99 时使用的总磁盘百分比:

 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();
         
     
 

这里是常用性能计数器的列表:

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

【讨论】:

这个给出了磁盘上的可用空间:PerformanceCounter("LogicalDisk", "FreeMegabytes", "C:")【参考方案3】:

请搜索 SO;有几个类似的问题:

How to get the CPU Usage C#

C#: List all processes and their current memory & CPU consumption?

How do I retrieve disk information in C#?

【讨论】:

如何获取磁盘使用情况? 通过性能计数器类。通过更改类别。

以上是关于在 C# 中获取当前 CPU、RAM 和磁盘驱动器的使用情况的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C#(托管代码)中获取 *THREAD* 的 CPU 使用率和/或 RAM 使用率?

如何在 C++ 中获取当前的 CPU 和 RAM 使用情况?

C# - 以百分比形式获取 GPU 的总使用量

我想知道(公式)如何计算名称节点、纱线和资源管理器的 RAM、CPU 和磁盘内存

如何将CPU,RAM,磁盘,碎片分配给ELK堆栈中的节点?

c#怎么获取程序当前运行路径