C# Program to Copy a File

C# provides built-in functionality for file operations through the System.IO namespace. File copying is a fundamental operation that allows you to duplicate existing files to new locations or with different names. The File.Copy() method offers a straightforward way to copy files with optional overwrite capabilities.

Syntax

The File.Copy() method has two overloads

public static void Copy(string sourceFileName, string destFileName);
public static void Copy(string sourceFileName, string destFileName, bool overwrite);

Parameters

  • sourceFileName The path and name of the file to copy.

  • destFileName The path and name of the destination file.

  • overwrite true to allow overwriting an existing destination file; false otherwise.

Using File.Copy() Without Overwrite

The basic version of File.Copy() copies a file from source to destination but throws an exception if the destination file already exists

using System;
using System.IO;

class Program {
   public static void Main() {
      string sourceFile = "source.txt";
      string destinationFile = "destination.txt";
      
      // Create a sample source file
      File.WriteAllText(sourceFile, "This is sample content for copying.");
      
      try {
         File.Copy(sourceFile, destinationFile);
         Console.WriteLine("File copied successfully!");
         
         // Verify the copy
         string content = File.ReadAllText(destinationFile);
         Console.WriteLine("Copied file content: " + content);
      }
      catch (IOException ex) {
         Console.WriteLine("Error: " + ex.Message);
      }
      
      // Clean up
      if (File.Exists(sourceFile)) File.Delete(sourceFile);
      if (File.Exists(destinationFile)) File.Delete(destinationFile);
   }
}

The output of the above code is

File copied successfully!
Copied file content: This is sample content for copying.

Using File.Copy() With Overwrite

The overloaded version with the overwrite parameter allows you to replace existing destination files

using System;
using System.IO;

class Program {
   public static void Main() {
      string sourceFile = "source.txt";
      string destinationFile = "destination.txt";
      
      // Create source and destination files
      File.WriteAllText(sourceFile, "New content to copy.");
      File.WriteAllText(destinationFile, "Old content to be replaced.");
      
      Console.WriteLine("Before copy - Destination content: " + File.ReadAllText(destinationFile));
      
      try {
         // Copy with overwrite enabled
         File.Copy(sourceFile, destinationFile, true);
         Console.WriteLine("File copied with overwrite!");
         
         Console.WriteLine("After copy - Destination content: " + File.ReadAllText(destinationFile));
      }
      catch (IOException ex) {
         Console.WriteLine("Error: " + ex.Message);
      }
      
      // Clean up
      if (File.Exists(sourceFile)) File.Delete(sourceFile);
      if (File.Exists(destinationFile)) File.Delete(destinationFile);
   }
}

The output of the above code is

Before copy - Destination content: Old content to be replaced.
File copied with overwrite!
After copy - Destination content: New content to copy.

Exception Handling

The File.Copy() method can throw several exceptions that should be handled properly

using System;
using System.IO;

class Program {
   public static void Main() {
      string sourceFile = "nonexistent.txt";
      string destinationFile = "destination.txt";
      
      try {
         File.Copy(sourceFile, destinationFile);
      }
      catch (FileNotFoundException) {
         Console.WriteLine("Source file not found.");
      }
      catch (DirectoryNotFoundException) {
         Console.WriteLine("Directory path not found.");
      }
      catch (UnauthorizedAccessException) {
         Console.WriteLine("Access denied - insufficient permissions.");
      }
      catch (IOException ex) {
         Console.WriteLine("IO Error: " + ex.Message);
      }
      catch (ArgumentException) {
         Console.WriteLine("Invalid file path or name.");
      }
   }
}

The output of the above code is

Source file not found.

Common Exceptions

Exception Description
FileNotFoundException Source file does not exist
DirectoryNotFoundException Invalid directory path specified
IOException Destination file exists (without overwrite) or I/O error
UnauthorizedAccessException Insufficient permissions to access files
ArgumentException Invalid characters in file path or name

Conclusion

The File.Copy() method in C# provides an efficient way to duplicate files with built-in error handling. Use the basic version when you want to prevent accidental overwrites, or the overloaded version with true parameter when you need to replace existing files. Always implement proper exception handling to manage potential file operation errors gracefully.

Updated on: 2026-03-17T07:04:36+05:30

553 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements