创建硬盘读写事件

发布于 2024-10-20 03:17:35 字数 163 浏览 6 评论 0原文

我正在尝试编写一些在硬盘读取数据或写入数据时触发事件的东西。我知道这涉及使用 System.Diagnostics.PerformanceCounter 但我对此了解不够,无法自己完成此操作。有人能指出我正确的方向吗?另外,我希望触发事件以返回正在读取或写入的驱动器。任何帮助将不胜感激。顺便说一句,这是 C#。

I am trying to write something that will fire an event anytime the hard disk reads data or writes data. I know this involves using System.Diagnostics.PerformanceCounter but I don't know this well enough to be able to do this on my own. Can someone point me in the right direction? Also, I'd like the event that fires to return which drive is being read or written to. Any help would be appreciated. This is C#, by the way.

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

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

发布评论

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

评论(1

千柳 2024-10-27 03:17:35

以下不会创建事件,但您可以将其与计时器一起使用以在托盘中显示信息(根据注释):

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

_Total 适用于所有磁盘...获取可用磁盘的特定实例名称使用:

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

然后,您可以为您感兴趣的每个实例创建一对 diskRead/diskWrite...有关如何将其与计时器结合使用的示例,请参阅< a href="http://miteshsureja.blogspot.de/2012/01/performance-counters-in-net.html" rel="noreferrer">这个。

The following does not create events but you could use it together with a timer to display information in the tray (as per comments):

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

_Total is for ALL disks... to get the specific instancenames of available disks use:

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

you can then create a pair of diskRead/diskWrite for every instance you are interested in... for sample on how to use this in combination with a timer see this.

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