C# - Copying the Contents From One File to Another File - GeeksforGeeks

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:
Place two files in your csharp executable folder in your system.In the main method use File.Copy() to copy contents from first file to second file.Display the text in file2 using File.ReadAllText() method.
Place two files in your csharp executable folder in your system.
In the main method use File.Copy() to copy contents from first file to second file.
Display the text in file2 using File.ReadAllText() method.
C#
// C# program to copy data from one file to anotherusing 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:
Picked
C#
C# Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Destructors in C#
Extension Method in C#
HashSet in C# with Examples
Top 50 C# Interview Questions & Answers
C# | How to insert an element in an Array?
Convert String to Character Array in C#
Socket Programming in C#
Program to Print a New Line in C#
Getting a Month Name Using Month Number in C#
Program to find absolute value of a given number