使用 C# 以编程方式确定计算机的最大硬盘数据传输速率

发布于 2024-09-07 15:40:14 字数 679 浏览 1 评论 0原文

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

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 来计算已用百分比,但我不知道如何读取磁盘的“理论”最大数据传输率。我需要它来计算所使用的磁盘传输率的百分比。 任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

焚却相思 2024-09-14 15:40:14

做到这一点的唯一方法就是亲自测试。您可以在应用程序开始时执行类似的操作:

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 目录位于相关磁盘上。如果没有,您必须在要测量的磁盘上创建一个路径。请注意,这是测量写入速度,而不是读取速度。

这有点做作,因为 1MB 写入的数据并不多,但您可以尝试使用更大的数据量;这个概念是一样的。

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.

夏至、离别 2024-09-14 15:40:14

尝试使用 WMI api,也 LINQ to WMI 可能会有所帮助。

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

如梦 2024-09-14 15:40:14

我不知道有什么方法可以获得硬盘的最大数据传输速率,但是通过WMI,您可以获得硬盘的接口(USB、IDE...)。您可以使用接口的最大数据传输速率并为其提供百分比。

另外,我将在此处留下一篇关于使用 WMI 从 HD 获取信息的小文章的链接,其中包含源代码。 链接

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文