Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a faster way to copy a file other than File.Copy

Tags:

c#

It takes about 2 minutes to do a File.Copy(src, dest); for a 1.6GB File from Folder A to Folder B on the same drive. Is there a faster way to do this in C#/.NET in code (w/o hardware) - Something with stream, threading etc?

Would a filestream be quicker? How about a class that chunks up the file using a threadpool, and reads a range of bytes/writes a range of bytes [that sounds like a great way to corrupt the file, but integrity is not priority 1 here, its speed :-)]

I have searched but everyone says use File.Copy, but it's slow (as slow as Windows Copy) - I would prefer not to use a 3rd party tool.


Here are some answers to some of the questions:

Copy time comparison:

> C# : 2.15m  
> Windows Explorer: 2.53m  
> TeraCopy: 2.26m
> FastCopy: 2.24m

Ok, those are not averages and I know they may change slightly on subsequent runs, but I really thought there would be a faster way to copy a file since I assumed that Windows was doing additional security and integrity checks :-(

I'm still hoping for some good answer (like 'oh yea, files over 1.5GB will be x faster if you do buffer m and turn off security n') -- ok, I'm just wishing at this point.

like image 650
schmoopy Avatar asked Aug 03 '10 17:08

schmoopy


People also ask

Is there a faster way to copy files?

Know Mouse Shortcuts for Faster Copying, Too But you can still use a few ways to copy and paste faster. Hold Ctrl and click multiple files to select them all, no matter where they are on the page. To select multiple files in a row, click the first one, then hold Shift while you click the last one.

Is moving a file faster than copying?

If we are cutting(moving) within a same disk, then it will be faster than copying because only the file path is modified, actual data is on the disk. If the data is copied from one disk to another, it will be relatively faster than cutting because it is doing only COPY operation.


2 Answers

If you are interested in creating a symbolic or hardlink instead of an actual copy then the following Windows APIs might be helpful.

  • CreateHardLink
  • CreateSymbolicLink
like image 91
Brian Gideon Avatar answered Nov 04 '22 17:11

Brian Gideon


You could try to use the hardware already at your disposal, and allocate a large buffer in memory. By reading and writing in larger chunks you might be able to remove some of the overhead.

However, there isn't much overhead to get rid of, the bottle neck is still the disk I/O. I would expect at best something like a 5% reduction in execution time.

like image 42
Guffa Avatar answered Nov 04 '22 18:11

Guffa