C# File Copy() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
System.IO

Definition

In C#, File.Copy() makes a copy of a file at a new path. The original stays where it is, and both files hold the same content.

By default the call fails if the destination file already exists. Pass true as the third argument when you want the old destination replaced. The destination folder must already exist; Copy does not create folders.

This is a static method of System.IO.File. Call it as File.Copy(...); there is no new File(). For an object that remembers one file, see FileInfo.

Syntax

Give the source path and the destination path. Add true to allow an overwrite.

C#
string source = Path.Combine(Path.GetTempPath(), "codetofun-a.txt");
string copy = Path.Combine(Path.GetTempPath(), "codetofun-b.txt");

File.WriteAllText(source, "data");
File.Copy(source, copy, true);
CallWhat it does
File.Copy(source, dest)Copies the file. Throws IOException if dest already exists.
File.Copy(source, dest, false)Same as above. false means "do not overwrite".
File.Copy(source, dest, true)Copies the file and replaces dest if it is already there.

File.Copy copies one file by path and returns nothing. FileInfo.CopyTo does the same job for an object and returns a FileInfo for the new copy.

Library declarations
C#
public static void Copy(string sourceFileName, string destFileName);
public static void Copy(string sourceFileName, string destFileName, bool overwrite);

Parameters

ParameterDescription
sourceFileNameThe file to copy. It must exist and must be a file, not a folder.
destFileNameThe path of the new copy. It must be a file name, not a folder, and its folder must exist.
overwriteOptional. true replaces an existing destination; false (the default) throws if it exists.

Return Value

Return ValueDescription
voidNothing is returned. When the call finishes without an exception, the copy exists.

C# Examples

Example 1 — Copy a File and Compare the Content

Copy one small file inside the temp folder and read both files.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-copy");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string source = Path.Combine(root, "original.txt");
        string target = Path.Combine(root, "backup.txt");

        File.WriteAllText(source, "hello");
        File.Copy(source, target);

        Console.WriteLine(File.Exists(source));
        Console.WriteLine(File.ReadAllText(target));
        Console.WriteLine(File.ReadAllText(source) == File.ReadAllText(target));

        Directory.Delete(root, true);
    }
}

How It Works

  1. A source to copy. WriteAllText creates original.txt with the text hello, so there is something real to copy.
  2. Copy keeps the original. After File.Copy the source is still on disk, and the first line prints True. That is the difference from Move.
  3. Same content. Reading backup.txt gives hello, and comparing the two strings with == prints True.

Example 2 — Overwrite an Existing Destination

Try to copy over a file that is already there, catch the error, and then repeat with true.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-copy-over");
        if (Directory.Exists(root)) Directory.Delete(root, true);
        Directory.CreateDirectory(root);

        string source = Path.Combine(root, "new.txt");
        string target = Path.Combine(root, "old.txt");
        File.WriteAllText(source, "new");
        File.WriteAllText(target, "old");

        try {
            File.Copy(source, target);
        } catch (IOException) {
            Console.WriteLine("Blocked: destination exists");
        }
        Console.WriteLine(File.ReadAllText(target));

        File.Copy(source, target, true);
        Console.WriteLine(File.ReadAllText(target));

        Directory.Delete(root, true);
    }
}

How It Works

  1. Two files, different text. new.txt holds new and old.txt holds old. The copy target already exists.
  2. The default refuses. Without the third argument Copy throws IOException. The catch block prints the message, and the target still reads old, so nothing was damaged.
  3. Allow it on purpose. With true the call replaces old.txt, and the last line prints new.

Example 3 — Copy Only the .txt Files to a Backup Folder

Loop over matching files and copy each one into a folder that was created first.

C#
using System;
using System.IO;

class Program {
    static void Main() {
        string root = Path.Combine(Path.GetTempPath(), "codetofun-file-copy-batch");
        if (Directory.Exists(root)) Directory.Delete(root, true);

        string src = Path.Combine(root, "docs");
        string dst = Path.Combine(root, "backup");
        Directory.CreateDirectory(src);
        Directory.CreateDirectory(dst);

        File.WriteAllText(Path.Combine(src, "a.txt"), "A");
        File.WriteAllText(Path.Combine(src, "b.txt"), "B");
        File.WriteAllText(Path.Combine(src, "c.log"), "C");

        foreach (string file in Directory.GetFiles(src, "*.txt")) {
            File.Copy(file, Path.Combine(dst, Path.GetFileName(file)));
        }

        string[] names = Directory.GetFiles(dst);
        Array.Sort(names, StringComparer.Ordinal);
        foreach (string name in names) {
            Console.WriteLine(Path.GetFileName(name));
        }

        Directory.Delete(root, true);
    }
}

How It Works

  1. Create both folders first. Copy does not make folders, so docs and backup are created before any file is copied.
  2. Pick files by pattern. GetFiles(src, "*.txt") returns only a.txt and b.txt. The .log file is left out.
  3. Build each destination. Path.GetFileName keeps just the name, and Path.Combine puts it inside backup. The new path needs a file name, not only a folder.
  4. Sort before printing. GetFiles promises no order, so the names are sorted with StringComparer.Ordinal. The output is then the same on every machine.

Frequently Asked Questions

Not by default. File.Copy(source, dest) throws IOException if dest exists. Use File.Copy(source, dest, true) when you want to replace it.
No. The folder must already exist, or the call throws DirectoryNotFoundException. Call Directory.CreateDirectory first when you are not sure.
No. It copies one file. To copy a folder, create the new folder, loop over Directory.GetFiles, and call File.Copy for each file and sub folder.
Copy leaves the original in place and makes a second file. Move transfers the file, so the original path no longer exists afterwards.
File.Copy throws FileNotFoundException. Check with File.Exists first, or catch the exception and show a helpful message.

Next: Create()

Create or overwrite a file and get a FileStream to write bytes into it.

Create() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful