使用C#以编程方式确定计算机的最大硬盘驱动器数据传输速率 [英] Determining a computer's maximum hard drive data transfer rate programmatically with C#

查看:150
本文介绍了使用C#以编程方式确定计算机的最大硬盘驱动器数据传输速率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用C#编写了一个小的WPF小部件,以三个小百分比类型的条形显示了当前的CPU活动,已使用的RAM和磁盘活动.我为此使用了以下PerformanceCounters:(diskCounter PerformanceCounter返回当前的总磁盘活动(以每秒字节数为单位))

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);
}

问题是,尽管我发现了如何获取总可用RAM来计算使用百分比的方法,但是我却找不到如何读取磁盘的理论"最大数据传输速率.我需要这个来计算所使用的磁盘传输率的百分比. 任何帮助将不胜感激.

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.

推荐答案

执行此操作的唯一方法是自己对其进行测试.您可以在应用程序的开头执行以下操作:

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

但是,这确实假定Temp目录位于相关磁盘上.如果不是,则必须在要测量的磁盘上创建路径.请注意,这是在测量写入速度,而不是读取速度.

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.

这有点人为,因为1MB的数据量不大,但是您可以尝试使用更大的数据量.这个概念是相同的.

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.

这篇关于使用C#以编程方式确定计算机的最大硬盘驱动器数据传输速率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆