Determining a computer's maximum hard drive data transfer rate programmatically with C#

久未见 提交于 2019-12-05 07:24:01

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.

Arsen Mkrtchyan

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!