使用PerformanceCounters定位特定的驱动器 [英] Using PerformanceCounters to target specific drives
问题描述
伙计们,我有以下代码:
Guys, I have the following code:
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个驱动器的总和.如何特别选择要监视的驱动器?
This code keeps track of Disk Reads per second and Disk Writes per second and it works fine. My question is, how do I keep track of reads and writes of one specific drive? I have 3 hard drives in my computer and right now its returning a total of all 3 drives combined. How can I specifically chose which drive I want to monitor?
推荐答案
您应将"_Total"替换为适当的驱动器号:
You should replace "_Total" with the appropriate drive number:
diskRead.InstanceName = "0";
应该检查一下.您需要指定名称,例如"0 C: D:"
. kes.
Should've checked that. You need to specify the name like "0 C: D:"
. Yikes.
您可以使用
var cat = new System.Diagnostics.PerformanceCounterCategory("PhysicalDisk");
var instNames = cat.GetInstanceNames();
过滤掉以数字开头的名称是绝对安全的. (_Total也在列表中.)
And it is probaly safe to filter out the names that start with a number. (_Total is also in the list).
这篇关于使用PerformanceCounters定位特定的驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!