Updated: July 2009
Copies an existing file to a new file. Overwriting a file of the same name is not allowed.
Namespace:
System.IO
Assembly:
mscorlib (in mscorlib.dll)
Visual Basic (Declaration)
Public Shared Sub Copy ( _
sourceFileName As String, _
destFileName As String _
)
Dim sourceFileName As String
Dim destFileName As String
File.Copy(sourceFileName, destFileName)
public static void Copy(
string sourceFileName,
string destFileName
)
public:
static void Copy(
String^ sourceFileName,
String^ destFileName
)
public static function Copy(
sourceFileName : String,
destFileName : String
)
Parameters
- sourceFileName
- Type: System..::.String
The file to copy.
- destFileName
- Type: System..::.String
The name of the destination file. This cannot be a directory or an existing file.
| Exception | Condition |
|---|
| UnauthorizedAccessException | The caller does not have the required permission. |
| 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 nullNothingnullptra null reference (Nothing in Visual Basic). |
| 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. -or- An I/O error has occurred. |
| NotSupportedException |
sourceFileName or destFileName is in an invalid format. |
This method is equivalent to the Copy(String, String, Boolean) method overload with the overwrite parameter set to false.
The sourceFileName and destFileName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.
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.
Dim sourceDir As String = "c:\current"
Dim backupDir As String = "c:\archives\2008"
Try
Dim picList As String() = Directory.GetFiles(sourceDir, "*.jpg")
Dim txtList As String() = Directory.GetFiles(sourceDir, "*.txt")
' Copy picture files.
For Each f As String In picList
'Remove path from the file name.
Dim fName As String = 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)
Next
' Copy text files.
For Each f As String In txtList
'Remove path from the file name.
Dim fName As String = 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 copyError As IOException
Console.WriteLine(copyError.Message)
End Try
Next
For Each f As String In txtList
File.Delete(f)
Next
For Each f As String In picList
File.Delete(f)
Next
Catch dirNotFound As DirectoryNotFoundException
Console.WriteLine(dirNotFound.Message)
End Try
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);
}
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
.NET Framework
Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
.NET Compact Framework
Supported in: 3.5, 2.0, 1.0
XNA Framework
Supported in: 3.0, 2.0, 1.0
Reference
Other Resources
Date | History | Reason |
|---|
July 2009
| Replaced the code example. |
Customer feedback.
|