硬盘创建活动的读写创建活动、硬盘

2023-09-03 13:17:52 作者:少年未安年已老/

我想写的东西,将触发事件随时硬盘读取数据或写入数据。我知道这涉及到使用System.Diagnostics.PerformanceCounter,但我不知道这足够好,能够做到这一点我自己。有人能指出我朝着正确的方向吗?另外,我想的事件,设备正在被读取或写入到火灾返回。任何帮助将是AP preciated。这是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.

推荐答案

以下不创建活动,但你可以用它连同一个计时器,在托盘显示信息(按评论):

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 是所有磁盘...获得可用磁盘的具体instancenames使用:

_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();

您可以再创建一个对 diskRead / diskWrite 的每一个实例你有兴趣...的关于如何使用这个组合具有定时看到这个的样本。

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.