C# Copy File

Summary: in this tutorial, you’ll learn how to copy an existing file to a new file using the C# File.Copy() method.

Introduction to the C# File.Copy() method

The C# File.Copy() method allows you to copy an existing file to a new file

public static void Copy (
   string sourceFileName, 
   string destFileName
);Code language: C# (cs)

In this syntax, the sourceFileName is the file to copy and the destFileName is the name of the destination file.

If the sourceFileName doesn’t exist, the Copy() method raises a FileNotFoundExcpetion.

If an I/O error occurs during the copying of the file e.g., the desFilename already exists, the File.Copy() method raises an IOException.

File.Copy() method example

The following example demonstrates how to copy readme.txt file from C:\temp directory to the C:\backup directory:

using static System.Console;

string sourceDir = @"C:\temp";
string backupDir = @"C:\backup";

string filename = @"readme.txt";


File.Copy(
    Path.Combine(sourceDir, filename),
    Path.Combine(backupDir, filename)
);Code language: C# (cs)

How it works.

First, define two variables that hold paths to the temp and backup directories:

string sourceDir = @"C:\temp";
string backupDir = @"C:\backup";Code language: C# (cs)

Second, declare a filename variable and initialize it with the “readme.txt“:

string filename = @"readme.txt";Code language: C# (cs)

Third, use the Path.Combine() method to create a path to the source and destination file and File.Copy() method to copy the readme.txt file for the C:\temp directory to the C:\backup directory:

File.Copy(
    Path.Combine(sourceDir, filename),
    Path.Combine(backupDir, filename)
);Code language: C# (cs)

When copying the file, an I/O error may occur. To handle it more gracefully, you can place the File.Copy() inside a try...catch statement like this:

using static System.Console;

string sourceDir = @"C:\temp";
string backupDir = @"C:\backup";

string filename = @"readme.txt";

try
{
    File.Copy(
        Path.Combine(sourceDir, filename),
        Path.Combine(backupDir, filename)
    );
}
catch (IOException ex)
{
    WriteLine(ex.Message);
}Code language: C# (cs)

For example, if the C:\backup\readme.txt already exists, you’ll get the following error message in the console:

Could not find file 'C:\temp\readme.txt'.Code language: C# (cs)

Similarly, if the C:\temp\readme.txt file does not exist, the program shows the following error message:

The file 'C:\backup\readme.txt' already exists.Code language: C# (cs)

Using File.Copy() to overwrite the destination file

If you want to overwrite the destination file if it exists, you can use an overload of the File.Copy() method:

public static void Copy (
   string sourceFileName, 
   string destFileName, 
   bool overwrite
);Code language: C# (cs)

When you set the overwrite argument to rue, the Copy() method will overwrite the destFileName file if it exists.

For example:

using static System.Console;

string sourceDir = @"C:\temp";
string backupDir = @"C:\backup";

string filename = @"readme.txt";

try
{
    File.Copy(
        Path.Combine(sourceDir, filename),
        Path.Combine(backupDir, filename),
        true
    );
}
catch (IOException ex)
{
    WriteLine(ex.Message);
}Code language: C# (cs)

In this example, we use File.Copy() method to copy the readme.txt from C:\temp directory to the C:\backup directory and overwrite if the C:\backup already has the readme.txt file.

Summary

  • Use File.Copy() method to copy an existing file to a new one and optionally overwrite the new file if it exists.
Was this tutorial helpful ?