File.Copy Method (String, String, Boolean)
Copies an existing file to a new file. Overwriting a file of the same name is allowed.
Assembly: mscorlib (in mscorlib.dll)
public static void Copy( string sourceFileName, string destFileName, bool overwrite )
Parameters
- sourceFileName
- Type: System.String
The file to copy.
- destFileName
- Type: System.String
The name of the destination file. This cannot be a directory.
- overwrite
- Type: System.Boolean
true if the destination file can be overwritten; otherwise, false.
| Exception | Condition |
|---|---|
| UnauthorizedAccessException |
The caller does not have the required permission. -or- destFileName is read-only. |
| ArgumentException |
sourceFileName or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars. -or- sourceFileName or destFileName specifies a directory. |
| ArgumentNullException |
sourceFileName or destFileName is null. |
| PathTooLongException |
The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters. |
| DirectoryNotFoundException |
The path specified in sourceFileName or destFileName is invalid (for example, it is on an unmapped drive). |
| FileNotFoundException |
sourceFileName was not found. |
| IOException |
destFileName exists and overwrite is false. -or- An I/O error has occurred. |
| NotSupportedException |
sourceFileName or destFileName is in an invalid format. |
The sourceFileName and destFileName parameters can specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory.
For a list of common I/O tasks, see Common I/O Tasks.
The following example copies files to the C:\archives\2008 backup folder. It uses the two overloads of the Copy method as follows:
-
It first uses the File.Copy(String, String) method overload to copy text (.txt) files. The code demonstrates that this overload does not allow overwriting files that were already copied.
It then uses the File.Copy(String, String, Boolean) method overload to copy pictures (.jpg files). The code demonstrates that this overload does allow overwriting files that were already copied.
string sourceDir = @"c:\current"; string backupDir = @"c:\archives\2008"; try { string[] picList = Directory.GetFiles(sourceDir, "*.jpg"); string[] txtList = Directory.GetFiles(sourceDir, "*.txt"); // Copy picture files. foreach (string f in picList) { // Remove path from the file name. string fName = f.Substring(sourceDir.Length + 1); // Use the Path.Combine method to safely append the file name to the path. // Will overwrite if the destination file already exists. File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName), true); } // Copy text files. foreach (string f in txtList) { // Remove path from the file name. string fName = f.Substring(sourceDir.Length + 1); try { // Will not overwrite if the destination file already exists. File.Copy(Path.Combine(sourceDir, fName), Path.Combine(backupDir, fName)); } // Catch exception if the file was already copied. catch (IOException copyError) { Console.WriteLine(copyError.Message); } } // Delete source files that were copied. foreach (string f in txtList) { File.Delete(f); } foreach (string f in picList) { File.Delete(f); } } catch (DirectoryNotFoundException dirNotFound) { Console.WriteLine(dirNotFound.Message); }
-
FileIOPermission
for reading from sourceFileName and writing to destFileName. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
- 12/27/2011
- Sune Foldager
if (File.Exists(newName))
{
File.Delete(newName);
}
File.Copy(oldName, newName);
At that point, the result is what I wanted.
- 11/4/2011
- John Simmons - outlaw programmer
- 4/25/2011
- MrJohnB
Be very careful with this method, as the Overwrite = True does NOT work properly.
I had an existing destination file that had some information inside it that was somehow preserved and carried over to the source file that was supposed to copy over it. This should be impossible, but I confirmed it for myself.
The details of my test were as follows... the test was done with an ESRI Map Document file (.mxd)
A map document file contains information about the map layers inside it, and what their datasources are.
When I copied a sourcefile mxd with certain layer datasource information to the destinationfile mxd, the bad datasource information from the destinationfile somehow made its way into the sourcefile that overwrote it. I tried this test over and over, and not until I deleted the destinationfile did the copy work properly and the destination file wound up with the proper datasource information.
And no, there were no exceptions thrown, and the source and destination file were not readonly nor had any other type of restrictions that could explain the observed behavior.
- 3/21/2011
- Harry Baals
- 3/21/2011
- Harry Baals