Question

My C# Windows service application copies a lot of files over network via SMB, FTP or SSH all day and night. To do the file copies I simply use System.IO.File.Copy. Which command does the .NET Framework execute here? Is it copy? Maybe xcopy? Is this different on different Windows versions? I also read about robocopy but it seems there is no API. Is it worth the effort using P/Invoke with xcopy or robocopy instead of simple copy? At the moment I doubt that for my case.

In a future release I have to copy all files in two remote directories instead of one. So what happens when I copy a file via

File.Copy("c:\test.txt", "\\server1\dir1\test.txt")

And then call

File.Copy("\\server1\dir1\test.txt", "\\server1\dir2\test.txt")

Will the files be copied twice from my server to the target server? As far as I understand my computer says to copy files from A to B so the files will be transfered from A to my machine to B, am I right?

Was it helpful?

Solution

To do the file copies I simply use System.IO.File.Copy. Which command does the .NET Framework execute here?

It uses the CopyFile routine in the windows API.

Will the files be copied twice from my server to the target server?

No, your example first copies from A to B and then from B to C. This is really inefficient because the second copy operation reads the file data back from B to A before it sends it to C. You should instead have two copy operations from "c:\test.txt" to two different destinations.

edit: I looked into it and it turns out that the file will not be read back to the local machine if you are copying within the same remote shared folder. So in your example where you are copying within the dir1 share, there is no problem. But as soon as you copy from one share to another, this optimization is lost and the file will be read to local memory during a copy operation.

OTHER TIPS

File.Copy() pinvokes the CopyFile() winapi function.

The xcopy.exe and robocopy.exe programs are separate utilities. They also use CopyFile(), their value-add is in handling more than one file at a time. Which of course has many complications beyond copying a single file at a time, they have lots of options to use wildcards, iterate sub-directories and making the copy conditional.

None of them have a special option to make duplicate copies. There is no point to it in general, just copy twice. The second copy will be a lot faster as long as the file isn't too big, the source file data will be present in the file system cache so doesn't have to be read from the disk again.

XCopy and robocopy are programs, and copy is command of windows' command line interpreter: cmd.exe (which is a program too).
.Net's File.Copy uses none of the above, and instead uses the win32api command CopyFile (as those programs probably do too).

So the answer of your question depends on the implementation of windows (which can change in future releases). The documentation does not state if the file is copied locally, but its data is almost certainly transmitted locally, and then sent to the other server (no need to save it on the local disk).
CopyFile

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top