Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to make a copy of an existing file
The File.Copy method in C# is used to copy an existing file from one location to another. This method is part of the System.IO namespace and provides a simple way to duplicate files programmatically.
Syntax
Following is the syntax for the File.Copy method −
File.Copy(string sourceFileName, string destFileName); File.Copy(string sourceFileName, string destFileName, bool overwrite);
Parameters
-
sourceFileName − The path of the file to copy.
-
destFileName − The path of the destination file.
-
overwrite − Optional boolean parameter. If true, overwrites the destination file if it already exists.
Using File.Copy to Copy a File
Here's a basic example of copying a file from one location to another −
using System;
using System.IO;
public class Program {
public static void Main() {
string sourcePath = @"D:\source.txt";
string destinationPath = @"D:\copy.txt";
// Create a sample file first
File.WriteAllText(sourcePath, "This is the original file content.");
// Copy the file
File.Copy(sourcePath, destinationPath);
Console.WriteLine("File copied successfully!");
Console.WriteLine("Source: " + sourcePath);
Console.WriteLine("Destination: " + destinationPath);
// Verify the copy by reading content
string copiedContent = File.ReadAllText(destinationPath);
Console.WriteLine("Copied file content: " + copiedContent);
}
}
The output of the above code is −
File copied successfully! Source: D:\source.txt Destination: D:\copy.txt Copied file content: This is the original file content.
Using File.Copy with Overwrite Option
When the destination file already exists, you need to specify the overwrite parameter −
using System;
using System.IO;
public class Program {
public static void Main() {
string sourcePath = @"D:\original.txt";
string destinationPath = @"D:\backup.txt";
// Create source file
File.WriteAllText(sourcePath, "Updated content for backup.");
// Create destination file that already exists
File.WriteAllText(destinationPath, "Old backup content.");
try {
// Copy with overwrite enabled
File.Copy(sourcePath, destinationPath, true);
Console.WriteLine("File copied and overwritten successfully!");
string newContent = File.ReadAllText(destinationPath);
Console.WriteLine("New backup content: " + newContent);
}
catch (Exception ex) {
Console.WriteLine("Error: " + ex.Message);
}
}
}
The output of the above code is −
File copied and overwritten successfully! New backup content: Updated content for backup.
Handling Exceptions
It's important to handle potential exceptions when copying files −
using System;
using System.IO;
public class Program {
public static void Main() {
string sourcePath = @"D:\document.txt";
string destinationPath = @"D:\document_copy.txt";
try {
// Create source file for demonstration
File.WriteAllText(sourcePath, "Important document content.");
// Check if source file exists
if (File.Exists(sourcePath)) {
File.Copy(sourcePath, destinationPath);
Console.WriteLine("File copied successfully!");
Console.WriteLine("File size: " + new FileInfo(destinationPath).Length + " bytes");
}
else {
Console.WriteLine("Source file does not exist.");
}
}
catch (UnauthorizedAccessException) {
Console.WriteLine("Access denied. Check file permissions.");
}
catch (DirectoryNotFoundException) {
Console.WriteLine("Directory not found. Check the path.");
}
catch (IOException ex) {
Console.WriteLine("IO Error: " + ex.Message);
}
}
}
The output of the above code is −
File copied successfully! File size: 27 bytes
Common Use Cases
-
Backup creation − Creating backups of important files before modifications.
-
File distribution − Copying files to multiple locations for distribution.
-
Template duplication − Creating copies of template files for customization.
-
Data migration − Moving files during system upgrades or migrations.
Conclusion
The File.Copy method provides a straightforward way to duplicate files in C#. Remember to handle exceptions properly and use the overwrite parameter when the destination file might already exist. Always verify that the source file exists before attempting to copy it.
