views:

303

answers:

3

I have written a small WPF widget using C# that displays the current CPU activity, RAM used and disk activity as three small percentage type bars. I have used the following PerformanceCounters for this: (diskCounter PerformanceCounter returns current total disk activity in bytes per second)

private void InitialisePerformanceCounters()
{
    cpuCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total", true);
    totalRam = (int)(new Microsoft.VisualBasic.Devices.ComputerInfo().TotalPhysicalMemory / 1024 / 1024);
    ramCounter = new PerformanceCounter("Memory", "Available MBytes");
    diskCounter = new PerformanceCounter("PhysicalDisk", "Disk Bytes/sec", "_Total", true);
}

The problem is that although I have discovered how to get the total available RAM to calculate a used percentage from, I cannot find out how to read the disk's 'theoretical' maximum data transfer rate. I need this to calculate the percentage of disk transfer rate used. Any help would be greatly appreciated.

+1  A: 

Try to use WMI api, also LINQ to WMI can be helpful.

ArsenMkrt
Thanks for the links... I've bookmarked them. :)
Sheridan
+1  A: 

The only way to do this would be to test it yourself. You could do something like this at the beginning of your application:

byte[] data = new byte[1024];

string path = System.IO.Path.GetTempFileName();

int bytesPerSecond = 0;

using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create))
{
    System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();

    watch.Start();

    for (int i = 0; i < 1024; i++) fs.Write(data, 0, data.Length);

    fs.Flush();

    watch.Stop();

    bytesPerSecond = (int)((data.Length * 1024) / watch.Elapsed.TotalSeconds);
}

System.IO.File.Delete(path);

This does, however, assume that the Temp directory is on the disk in question. If not, you'll have to create a path on the disk you want to measure. Note that this is measuring write speed, not read speed.

This is somewhat contrived since 1MB is not much data to write, but you could try it with a larger amount of data; the concept is the same.

Adam Robinson
@Adam how will you find the max possible rate? AFAIK this depends on the current position of the HD, so this will more or less give you a wrong result, or not?
InsertNickHere
@InsertNickHere: The only way to measure the maximum possible rate would be to write to the entire disk and measure in intervals. You're correct in that this depends on a number of factors, but this should give at least a usable approximation.
Adam Robinson
Thanks Adam, but I don't need anything so accurate... the theoretical max rate suggested by Jonathan is fine.
Sheridan
After looking into Jonathon's suggestion further, it seems that I still cannot get the transfer rate using his method. It seems that this method may be most suitable after all... thanks again.
Sheridan
how can I use 3MB file to test it ?
pedram
+1  A: 

I Don't know any way to obtain the maximum data transfer rate of a HD, but with WMI you can obtain the intarface of the hard disk (USB,IDE...). You could use the maximum data transfer rate of the interface and reffers your percentage to it.

Also, I'm going to leave a link here to a little article about obtain info frm the HD using WMI, with source code. LINK

Jonathan
Thanks, this sounds like what I'm after. Thanks also for the link, but if you're talking about the Win32_LogicalDisk class, I haven't found the suitable field/attribute that provides that information... would you happen to know it?
Sheridan
I think its InterfaceType - http://msdn.microsoft.com/en-us/library/aa394132(VS.85).aspx Here is an example: http://www.geekpedia.com/tutorial73_An-introduction-in-retrieving-WMI-in-Csharp.html
SwDevMan81
Thanks SwDevMan81, I'll give it a go.
Sheridan
I've found a problem with this method... InterfaceType will tell me if a drive is USB, but not which version and therefore maximum theoretical data transfer rate. Similarly, it will tell me 1394 for Firewire, but not 400 or 800 and IDE does not relay the spin speed.Thanks still for the links... very useful.
Sheridan