Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

C# – Copying the Contents From One File to Another File

  • Last Updated : 16 Nov, 2021

Given a file, now our task is to copy data from one file to another file using C#. So to do this task we use the Copy() method of the File class from the System.IO namespace. This function is used to copy content from one file to a new file. It has two different types of overloaded methods:

1. Copy(String, String): This function is used to copy content from one file to a new file. It does not support overwriting of a file with the same name. 

Syntax:

File.Copy(file1, file2);

Where file1 is the first file and file2 is the second file. 

Exceptions: This method will throw the following exceptions:

  • UnauthorizedAccessException: This exception will occur when the caller does not have the required permission.
  • ArgumentException: This exception will occur when file1 or file2 specifies a directory.
  • ArgumentNullException: This exception will occur when file1 or file2 is null.
  • PathTooLongException: This exception will occur when the specified path, file name, or both exceed the system-defined maximum length.
  • DirectoryNotFoundException: This exception will occur when the path specified in file1 or file2 is invalid.
  • FileNotFoundException: This exception will occur when file1 was not found.
  • IOException: This exception will occur when file2 exists.
  • NotSupportedException: This exception will occur when file1 or file2 is in an invalid format.

2. Copy(String, String, Boolean): This function is used to copy content from one file to a new file. It does not support overwriting of a file with the same name. 

Syntax:

File.Copy(file1, file2, owrite);

Where file1 is the first file, file2 is the second file, and write is a boolean variable if the destination file can be overwritten then it is set to true otherwise false. 

Exceptions: This method will throw the following exceptions:

  • UnauthorizedAccessException: This exception will occur when the caller does not have the required permission. Or the file2 is readonly or write is set to true and file to is hidden but file1 is not hidden.
  • ArgumentException: This exception will occur when file1 or file2 specifies a directory.
  • ArgumentNullException: This exception will occur when file1 or file2 is null.
  • PathTooLongException: This exception will occur when the specified path, file name, or both exceed the system-defined maximum length.
  • DirectoryNotFoundException: This exception will occur when the path specified in file1 or file2 is invalid.
  • FileNotFoundException: This exception will occur when file1 was not found.
  • IOException: This exception will occur when file2 exists and owrite is false.
  • NotSupportedException: This exception will occur when file1 or file2 is in an invalid format.

Example:

Let us consider two files named file1 and file2. Now the file1.txt contains the following text:

Now the file2.txt contains the following text:

Approach:

  1. Place two files in your csharp executable folder in your system.
  2. In the main method use File.Copy() to copy contents from first file to second file.
  3. Display the text in file2 using File.ReadAllText() method.

C#




// C# program to copy data from one file to another
using System;
using System.IO;
  
class GFG{
      
static void Main()
{
    
    // Copy contents from file1 to file2
    File.Copy("file1.txt", "file2.txt");
      
    // Display file2 contents
    Console.WriteLine(File.ReadAllText("file2.txt"));
}
}

Output:

Now, file2.txt is:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!