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?
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.