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
How to copy files into a directory in C#?
To copy files in C#, the File.Copy method is the primary approach. This method allows you to copy individual files from one location to another, with options to control whether existing files should be overwritten.
Syntax
The File.Copy method has two overloads −
File.Copy(string sourceFileName, string destFileName)
File.Copy(string sourceFileName, string destFileName, bool overwrite)
Parameters
sourceFileName − The file to copy.
destFileName − The name of the destination file. This cannot be a directory.
overwrite −
trueif the destination file should be overwritten if it already exists; otherwise,false.
Copy Methods Comparison
| Method | Overwrite Behavior | Exception on Existing File |
|---|---|---|
| Copy(String, String) | Not allowed | Throws IOException if destination exists |
| Copy(String, String, Boolean) | Controlled by boolean parameter | Only if overwrite is false and file exists |
Using Directory.GetFiles for Multiple Files
To copy multiple files, combine Directory.GetFiles with File.Copy. The Directory.GetFiles method returns file paths that match specified patterns −
Directory.GetFiles(string path, string searchPattern, SearchOption searchOption)
Example − Copying Multiple Files
using System;
using System.IO;
class Program {
static void Main(string[] args) {
// Create test directories and files
string sourceDir = @"C:\temp\source";
string destDir = @"C:\temp\destination";
Directory.CreateDirectory(sourceDir);
Directory.CreateDirectory(destDir);
// Create sample files
File.WriteAllText(Path.Combine(sourceDir, "file1.txt"), "Content of file 1");
File.WriteAllText(Path.Combine(sourceDir, "file2.txt"), "Content of file 2");
File.WriteAllText(Path.Combine(destDir, "existing.txt"), "Existing file");
Console.WriteLine("-------------Source Folder-------------");
string[] sourceFiles = Directory.GetFiles(sourceDir, "*.*", SearchOption.TopDirectoryOnly);
foreach (string file in sourceFiles) {
Console.WriteLine(Path.GetFileName(file));
}
Console.WriteLine("
-------------Destination Folder Before Copying-------------");
string[] destFilesBefore = Directory.GetFiles(destDir, "*.*", SearchOption.TopDirectoryOnly);
foreach (string file in destFilesBefore) {
Console.WriteLine(Path.GetFileName(file));
}
// Copy all files from source to destination
foreach (string file in sourceFiles) {
string fileName = Path.GetFileName(file);
string destPath = Path.Combine(destDir, fileName);
File.Copy(file, destPath, true); // true allows overwriting
}
Console.WriteLine("
-------------Destination Folder After Copying-------------");
string[] destFilesAfter = Directory.GetFiles(destDir, "*.*", SearchOption.TopDirectoryOnly);
foreach (string file in destFilesAfter) {
Console.WriteLine(Path.GetFileName(file));
}
}
}
The output of the above code is −
-------------Source Folder------------- file1.txt file2.txt -------------Destination Folder Before Copying------------- existing.txt -------------Destination Folder After Copying------------- existing.txt file1.txt file2.txt
Using File.Copy with Error Handling
Example − Safe File Copying
using System;
using System.IO;
class Program {
static void Main(string[] args) {
// Create test setup
string sourceDir = @"C:\temp\test_source";
string destDir = @"C:\temp\test_dest";
Directory.CreateDirectory(sourceDir);
Directory.CreateDirectory(destDir);
string sourceFile = Path.Combine(sourceDir, "document.txt");
File.WriteAllText(sourceFile, "Important document content");
string destFile = Path.Combine(destDir, "document_copy.txt");
try {
// Copy without overwrite
File.Copy(sourceFile, destFile, false);
Console.WriteLine("File copied successfully to: " + destFile);
// Attempt to copy again without overwrite - will throw exception
File.Copy(sourceFile, destFile, false);
}
catch (IOException ex) {
Console.WriteLine("Copy failed: " + ex.Message);
// Copy with overwrite allowed
File.Copy(sourceFile, destFile, true);
Console.WriteLine("File copied with overwrite enabled");
}
catch (Exception ex) {
Console.WriteLine("Unexpected error: " + ex.Message);
}
}
}
The output of the above code is −
File copied successfully to: C:\temp\test_dest\document_copy.txt Copy failed: The file 'C:\temp\test_dest\document_copy.txt' already exists. File copied with overwrite enabled
Conclusion
The File.Copy method in C# provides a straightforward way to copy files, with the boolean overload offering control over existing file behavior. When copying multiple files, combine it with Directory.GetFiles and always include proper exception handling for robust file operations.
