How to get the CPU, RAM and Disk drive usage of the system in C# code?
如何在c#代码中获得系统的CPU、RAM和磁盘驱动器使用率?
2 个解决方案
#1
22
Please search SO; there are several similiar questions:
请搜索;有几个相似的问题:
-
如何获得CPU使用c#
-
C#: List all processes and their current memory & CPU consumption?
列出所有进程和它们当前的内存和CPU消耗?
-
How do I retrieve disk information in C#?
如何在c#中检索磁盘信息?
#2
3
Here is a solution which will output disk usage, the total disk percent being used at the time that Timer99 is polled:
这里有一个将输出磁盘使用情况的解决方案,即在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();
}
}
}
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);
#1
22
Please search SO; there are several similiar questions:
请搜索;有几个相似的问题:
-
如何获得CPU使用c#
-
C#: List all processes and their current memory & CPU consumption?
列出所有进程和它们当前的内存和CPU消耗?
-
How do I retrieve disk information in C#?
如何在c#中检索磁盘信息?
#2
3
Here is a solution which will output disk usage, the total disk percent being used at the time that Timer99 is polled:
这里有一个将输出磁盘使用情况的解决方案,即在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();
}
}
}
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);