Replacing File.Copy
The easiest way to copy a file in a .NET program is to call the File.Copy method, supplying it the source and destination files. It could hardly be simpler than this:
File.Copy(srcFilename, destFilename);
That method will throw IOException if there is an existing file of the same name as destFilename. If you want to overwrite existing files, call the overload and specify true for the overwrite parameter:
File.Copy(srcFilename, destFilename, true);
File.Copy certainly is very easy to use, but as I mentioned in the previous section, it suffers from the large file copy problem. That is, using File.Copy to copy a very large file from one machine to another can cause the source machine to run out of memory. For example, I encountered that problem when executing a statement of this form:
File.Copy(@"\\server\data\file.dat", @"d:\data\file.dat", true);
If you want to copy a file and not encounter those problems, you have to write your own copy method. Read more »
