Click here to Skip to main content
15,891,925 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..
Posted

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900