在 C# 中获取当前的 CPU、RAM 和磁盘驱动器使用情况
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1253154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current CPU, RAM and Disk drive usage in C#
提问by Sauron
How to get the CPU, RAM and Disk drive usage of the system in C# code?
如何在 C# 代码中获取系统的 CPU、RAM 和磁盘驱动器使用情况?
采纳答案by Mitch Wheat
Please search SO; there are several similiar questions:
请搜索SO; 有几个类似的问题:
回答by pollaris
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);
回答by Fernandes
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();
}