File.Copy
overwrite issue
file handling
programming error
C#

'File.Copy' does not overwrite a file

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In C#, File.Copy does not overwrite an existing destination file unless you explicitly ask it to. That behavior surprises a lot of people because the method name sounds unconditional, but the default overload is intentionally conservative and throws if the target already exists.

The Default Behavior

This overload does not overwrite:

csharp
using System.IO;

File.Copy("report.txt", "backup.txt");

If backup.txt already exists, .NET throws an IOException. That is expected behavior, not a bug.

The reason is safety. File-copy operations are destructive if they silently replace data, so the API makes overwrite an explicit choice.

Use the Overwrite Parameter

If you want replacement behavior, use the overload with the third argument:

csharp
using System.IO;

File.Copy("report.txt", "backup.txt", overwrite: true);

That tells .NET to replace the destination file if it already exists.

This is the most direct fix for the “does not overwrite” problem.

Equivalent Pattern With FileInfo

If you are already working with FileInfo, the same idea exists there:

csharp
1using System.IO;
2
3var source = new FileInfo("report.txt");
4source.CopyTo("backup.txt", overwrite: true);

Choose whichever style matches the rest of your code. The important part is the explicit overwrite flag.

When Overwrite Still Fails

Even with overwrite: true, copying can still fail for other reasons:

  • the destination file is locked by another process
  • the application lacks permission to write the destination
  • the source path is wrong
  • the destination path points to a directory rather than a file

A defensive example:

csharp
1using System;
2using System.IO;
3
4try
5{
6    File.Copy("report.txt", "backup.txt", overwrite: true);
7    Console.WriteLine("Copy complete.");
8}
9catch (UnauthorizedAccessException ex)
10{
11    Console.WriteLine("Permission error: " + ex.Message);
12}
13catch (IOException ex)
14{
15    Console.WriteLine("I/O error: " + ex.Message);
16}

This does not prevent failures, but it makes the cause visible instead of leaving you with a silent crash.

Alternative: Delete Then Copy

Some developers write:

csharp
1if (File.Exists("backup.txt"))
2{
3    File.Delete("backup.txt");
4}
5
6File.Copy("report.txt", "backup.txt");

This works, but it is usually worse than overwrite: true because it creates a gap where the destination file has been deleted but the new copy has not yet succeeded. If the copy fails, you end up with nothing at the destination.

The overwrite overload is simpler and usually safer.

Be Careful With Relative Paths

Another source of confusion is path resolution. You may think backup.txt exists, but your program may be running in a different working directory than expected.

Check full paths when debugging:

csharp
1using System;
2using System.IO;
3
4string source = Path.GetFullPath("report.txt");
5string destination = Path.GetFullPath("backup.txt");
6
7Console.WriteLine(source);
8Console.WriteLine(destination);

Sometimes the problem is not overwriting at all. The program is simply copying to a different folder than you thought.

Consider Atomic Replace for Critical Files

If you are updating an important file and need a safer replace flow, File.Replace may be more appropriate than File.Copy.

csharp
using System.IO;

File.Replace("new-report.txt", "report.txt", "report.bak");

That is not the same operation as a plain copy, but it is worth knowing when the goal is controlled replacement with a backup.

Common Pitfalls

The biggest pitfall is using the two-argument overload and expecting overwrite behavior. By design, it throws if the destination already exists.

Another common issue is forgetting that overwrite does not bypass locks or permissions. A file in use by another process can still cause the copy to fail.

People also often delete the destination manually before copying. That works, but it is less robust than using the built-in overwrite parameter.

Finally, always confirm the effective paths. Relative paths cause a lot of fake “overwrite” bugs that are really just wrong-directory bugs.

Summary

  • 'File.Copy(source, dest) does not overwrite existing files.'
  • Use File.Copy(source, dest, true) when replacement is intended.
  • Overwrite can still fail because of locks, permissions, or bad paths.
  • Prefer the overwrite overload over manual delete-then-copy logic.
  • Check full paths when the behavior seems inconsistent.

Course illustration
Course illustration

All Rights Reserved.