使用 PerformanceCounters 来定位特定驱动器

发布于 2024-10-19 17:46:15 字数 545 浏览 5 评论 0原文

伙计们,我有以下代码:

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 个硬盘的总和。如何具体选择要监控的驱动器?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月朦胧 2024-10-26 17:46:15

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

  diskRead.InstanceName = "0";

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

编辑2:

您可以使用 获取名称

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

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

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).

杀手六號 2024-10-26 17:46:15

使用特定的 InstanceName,而不是 _Total。使用 Perfmon.exe 查找实例名称。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文