c# - 使用 PerformanceCounters 定位特定驱动器

标签 c# .net performancecounter

伙计们,我有以下代码:

using System.Diagnostics;

private PerformanceCounter diskRead = new PerformanceCounter();
private PerformanceCounter diskWrite = new PerformanceCounter();

diskRead.CategoryName = "PhysicalDisk";
diskRead.CounterName = "Disk Reads/sec";
diskRead.InstanceName = "_Total";

diskWrite.CategoryName = "PhysicalDisk";
diskWrite.CounterName = "Disk Writes/sec";
diskWrite.InstanceName = "_Total";

此代码跟踪每秒的磁盘读取次数和每秒的磁盘写入次数,并且运行良好。我的问题是,如何跟踪一个特定驱动器的读写?我的计算机中有 3 个硬盘驱动器,现在它返回的是所有 3 个驱动器的总和。如何专门选择要监控的驱动器?

最佳答案

您应将“_Total”替换为适当的驱动器编号:

<罢工>

<罢工>
  diskRead.InstanceName = "0";

<罢工>

应该检查一下。您需要指定名称,如 "0 C: D:" .哎呀。

编辑 2:

你可以用

获取名字
    var cat = new System.Diagnostics.PerformanceCounterCategory("PhysicalDisk");
    var instNames = cat.GetInstanceNames();

过滤掉以数字开头的名称可能是安全的。 (_Total 也在列表中)。

关于c# - 使用 PerformanceCounters 定位特定驱动器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5172555/

相关文章:

ARM Cortex-a9 事件计数器返回 0

graphics - GPU 像素和 Texel 写入速度

c# - Redis - 从 IIS session 状态移动

c# - Asp.net 核心领域本地化

.net - C# lambda 分配和收集

.net - 以编程方式在 Excel 中删除 "The number in this cell is formatted as text or preceded by an apostrophe"

windows - QueryPerformanceCounter 和线程安全

c# - 防止.net ide在中断时跳转到当前执行行

c# - C# 编译器如何决定发出可重定向的程序集引用?

c# - C# 的 F# Xml TypeProvider