C# File.Copy Examples

File with lines of text

You need to copy a file from one location to another, leaving both copies on the disk. You can use File.Copy from System.IO to do this. This document breaks down three common File.Copy usages, and warns you of potential errors, using the C# programming language.

This C# article looks at the File.Copy method in the System.IO namespace.

Example 1

Here we want to copy one file to a new location, one where no file exists. This is straightforward and you will see that both files in the example, file-a.txt and file-new.txt, have the same contents.

Program that uses File.Copy [C#]

using System;
using System.IO;

class Program
{
    static void Main()
    {
	// Figure 1
	// Copy one file to a non-existent location
	File.Copy("file-a.txt", "file-new.txt");

	// Display the contents of both files
	Console.WriteLine(File.ReadAllText("file-a.txt"));
	Console.WriteLine(File.ReadAllText("file-new.txt"));
    }
}

Output

Contents of File A.
Contents of File A.

Description. This C# console program takes a file that exists, file-a.txt, and copies it to file-new.txt. The final two statements show that both files contain the same string.

Example 2

Here we find that File.Copy will cause an exception if you use it when the target destination has a file there already. To solve this problem, you must specify the third parameter of File.Copy to be true.

Program that uses File.Copy parameters [C#]

using System;
using System.IO;

class Program
{
    static void Main()
    {
	// Write initial contents of File B.
	Console.WriteLine(File.ReadAllText("file-b.txt"));
	// "File B contents."

	// COPY:
	// Copy one file to a location where there is a file.
	File.Copy("file-a.txt", "file-b.txt", true); // overwrite = true

	// Display the contents of both files
	Console.WriteLine(File.ReadAllText("file-a.txt"));
	Console.WriteLine(File.ReadAllText("file-b.txt"));
    }
}

Output

"Contents of File A."
"Contents of File A."

Description. First the example displays the contents of file-b.txt. Then it calls File.Copy and copies file-a.txt to file-b.txt. No exception is thrown here. Finally, it displays the contents of the two files, which are equal now.

Example 3

Here we see that File.Copy will throw FileNotFoundException and IOException if there are certain problems. It will also throw other exceptions that I do not show here.

Program that demonstrates File.Copy exceptions [C#]

using System;
using System.IO;

class Program
{
    static void Main()
    {
	// Copying a file that doesn't exist:
	try
	{
	    File.Copy("file-missing.txt", "file-new.txt");
	}
	catch (Exception ex)
	{
	    Console.WriteLine(ex);
	    // System.IO.FileNotFoundException: Could not find file 'file-missing.txt'.
	}

	// Copying to a file without overwrite
	try
	{
	    File.Copy("file-a.txt", "file-b.txt");
	}
	catch (Exception ex)
	{
	    Console.WriteLine(ex);
	    // System.IO.IOException: The file 'file-b.txt' already exists.
	}
    }
}

Description. The program has two try/catch blocks. The first one protects the program from a call to File.Copy that tries to copy a missing file. It demonstrates that the FileNotFoundException is thrown. The second try/catch block demonstrates that the IOException is thrown when you try to copy to a file that exists, but don't specify overwrite. To specify overwrite, use true as the third parameter.

Exceptions

Warning

File IO will always throw exceptions occasionally. It is one of the best uses for exception-handling. You should wrap your File.Copy, as well as other methods such as File.ReadAllText, inside a try/catch block. Figure 3 shows a basic example of this.

Related methods

Programming tip

There are several related methods in the System.IO.File class, such as File.Move and File.Replace. The File.Replace combine File.Copy with a deletion of the file. You can duplicate its functionality with File.Copy and then File.Delete. The File.Move method does a filesystem rename instead of copying any data.

File.Move Method, Rename File

Summary

The C# programming language

Here we saw two useful ways to use File.Copy in the C# language, and saw examples of two ways it can throw exceptions. It is important that you handle errors when using File.Copy, or it may bring down your program.

File Handling
.NET