Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Events for hard disk read and write

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.

like image 961
Icemanind Avatar asked Mar 01 '11 23:03

Icemanind


1 Answers

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.

like image 157
Yahia Avatar answered Sep 23 '22 13:09

Yahia