如何在c#File.Copy中获得文件传输速度?! [英] How to get file transfer speed in c# File.Copy ?!

查看:395
本文介绍了如何在c#File.Copy中获得文件传输速度?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个简单的文件传输应用程序,用于使用usb设备进行快速文件传输,为了使它更有趣,我想在屏幕上添加一个传输速度,就像在Windows中的正常文件传输窗口中一样转移的速度,我想在我的应用程序中有相同的东西,有没有办法做到这一点?

im copy with File.Copy(); ..提前致谢..

I made a simple file transfer application for quick file transfer with usb device, and to make it more fun, i would like to add to the screen a transfer speed, just like in the normal file transfer window in windows where it shows a speed of the transfer, i would like to have the same thing in mine application, is there any way to do this?
im copying with File.Copy(); .. Thanks in advance..

推荐答案

您只能在点击之前和之后检查时间 File.Copy ;你不能挂钩到转移的中间来更新部分速度。如果这就是你想要的,你可以使用类 System.Diagnostic.Stopwatch ,它可以提供最佳的准确度: https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v= vs.110%29.aspx



如果你想测量一些大文件副本中间的速度,你需要写您自己的文件复制方法,可以使用类 System.IO.BinaryReader System.IO.BinaryWriter :

https: //msdn.microsoft.com/en-us/library/system.io.binaryreader%28v=vs.110%29.aspx

https://msdn.microsoft.com/en-us/library/system.io。 binarywriter%28v = vs.110%29.aspx



你可以按一定大小的块读取和写入数据,并在每个块之后检查速度。你应该更加小心:你很容易损害你的表现。所以,你可能想要试验一下,与 File.Copy 的速度进行比较并尝试不同的块大小。期待非常高的速度统计分散,所以重复测试多次并收集可靠的统计数据,然后再做出任何结论。



有时间捕获:您需要从时间中排除JIT编译的时间。这是一个需要考虑的非常重要的因素。这很容易做到。请记住,如果某些代码第一次执行,它就不能进行JIT编译。因此,在至少一次调用所有方法之后,通过调用开始计时。



参见: http://en.wikipedia.org/wiki/Just-in-time_compilation



-SA
You can only check timing before and after the click to File.Copy; you cannot hook into the middle of transfer to update partial speeds. If this is all you want, you can use the class System.Diagnostic.Stopwatch, which gives you the best accuracy: https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx.

If you want to measure the speed in the middle of copy of some big file, you would need to write your own file copy method, which can be easily done using the classes System.IO.BinaryReader and System.IO.BinaryWriter:
https://msdn.microsoft.com/en-us/library/system.io.binaryreader%28v=vs.110%29.aspx,
https://msdn.microsoft.com/en-us/library/system.io.binarywriter%28v=vs.110%29.aspx.

You can read and write data by blocks of some size and checkup the speed after each block. You should better be careful with that: you can easily compromise your performance. So, you may want to experiment with that, comparing with the speed of File.Copy and trying different block sizes. Expect very high dispersion of statistics of the speeds, so repeat test many times and collect solid statistics, before making any conclusions.

There is on catch in timing: you need to exclude the time of JIT compilation from your timing. This is a very important factor to take into account. And this is easy enough to do. Just keep in mind that if some code is executed for the very first time, it could not be JIT-compiled yet. So, start your timing with the call when all the methods have been already called before at least once.

See also: http://en.wikipedia.org/wiki/Just-in-time_compilation.

—SA


这篇关于如何在c#File.Copy中获得文件传输速度?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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