'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:
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:
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:
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:
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:
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:
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.
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.

