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.
Two files, different text.new.txt holds new and old.txt holds old. The copy target already exists.
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.
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);
}
}
Output
a.txt
b.txt
How It Works
Create both folders first.Copy does not make folders, so docs and backup are created before any file is copied.
Pick files by pattern.GetFiles(src, "*.txt") returns only a.txt and b.txt. The .log file is left out.
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.
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.