Using PerformanceCounters to target specific drives

眉间皱痕 提交于 2019-12-06 03:31:21

问题


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

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?


回答1:


You should replace "_Total" with the appropriate drive number:

  diskRead.InstanceName = "0";

Should've checked that. You need to specify the name like "0 C: D:". Yikes.

Edit 2:

You can get the names with

    var cat = new System.Diagnostics.PerformanceCounterCategory("PhysicalDisk");
    var instNames = cat.GetInstanceNames();

And it is probaly safe to filter out the names that start with a number. (_Total is also in the list).




回答2:


Use a specific InstanceName, not _Total. Use Perfmon.exe to find the instance names.



来源:https://stackoverflow.com/questions/5172555/using-performancecounters-to-target-specific-drives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!