File.Copy(String, String, Boolean) Method in C# with Examples
Last Updated :
28 Apr, 2025
File.Copy(String, String, Boolean) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file if exist, else create a new destination file then copying process is done.
Syntax:
public static void Copy (string sourceFileName, string destFileName, bool overwrite);
Parameter: This function accepts three parameters which are illustrated below:
- sourceFileName: This is the file from where data is copied.
- destFileName: This is the file where data is pasted.
- overwrite: This is the boolean value. It uses true if the destination file can be overwritten otherwise it uses false.
Exceptions:
- UnauthorizedAccessException: The destFileName is read-only OR Here overwrite is true if the destFileName exists and is hidden, but source filename is not hidden.
- ArgumentException: The source filename or destFileName is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars. OR the sourceFileName or destFileName specifies a directory.
- ArgumentNullException: The sourceFileName or destFileName is null.
- PathTooLongException: The specified path, file name, or both exceed the system-defined maximum length.
- DirectoryNotFoundException: The path specified in source filename or destFileName is invalid (for example, it is on an unmapped drive).
- FileNotFoundException: The source filename was not found.
- IOException: The destFileName exists and overwrite is false OR An I/O error has occurred.
- NotSupportedException: The sourceFileName or destFileName is in an invalid format.
Below are the programs to illustrate the File.Copy(String, String, Boolean) method.
Program 1: Before running the below code, two files i.e, source file file.txt and destination file gfg.txt are created with some contents shown below:
C#
// C# program to illustrate the usage
// of File.Copy() method
// Using System, System.IO,
// System.Text and System.Linq namespaces
using System;
using System.IO;
using System.Text;
using System.Linq;
class GFG {
// Main() method
public static void Main()
{
// Specifying two files
string sourceFile = @"file.txt";
string destinationFile = @"gfg.txt";
try {
// Copying source file's contents to
// destination file
File.Copy(sourceFile, destinationFile, true);
}
catch (IOException iox) {
Console.WriteLine(iox.Message);
}
Console.WriteLine("Copying process has been done.");
}
}
Executing:
mcs -out:main.exe main.cs
mono main.exe
Copying process has been done.
After running the above code, above output is shown and the destination file contents become like shown below:
Program 2: Before running the below code, two files i.e, source file file.txt and destination file gfg.txt is created with some contents shown below:
C#
// C# program to illustrate the usage
// of File.Copy() method
// Using System, System.IO,
// System.Text and System.Linq namespaces
using System;
using System.IO;
using System.Text;
using System.Linq;
class GFG {
// Main() method
public static void Main()
{
// Specifying two files
string sourceFile = @"file.txt";
string destinationFile = @"gfg.txt";
try {
// Copying source file's contents to
// destination file
File.Copy(sourceFile, destinationFile, true);
}
catch (IOException iox) {
Console.WriteLine(iox.Message);
}
Console.WriteLine("Copying process has been done.");
}
}
Executing:
mcs -out:main.exe main.cs
mono main.exe
Copying process has been done.
After running the above code, above output is shown and the destination file contents get overwritten with the content of source file file.txt like shown below:
Program 3: Before running the below code, two files i.e, source file file.txt and destination file gfg.txt is created with some contents shown below:
C#
// C# program to illustrate the usage
// of File.Copy() method
// Using System, System.IO,
// System.Text and System.Linq namespaces
using System;
using System.IO;
using System.Text;
using System.Linq;
class GFG {
// Main() method
public static void Main()
{
// Specifying two files
string sourceFile = @"file.txt";
string destinationFile = @"gfg.txt";
try {
// Copying source file's contents to
// destination file
File.Copy(sourceFile, destinationFile, false);
}
catch (IOException iox) {
Console.WriteLine(iox.Message);
}
}
}
Executing:
mcs -out:main.exe main.cs
mono main.exe
Could not create file "/home/runner/NutritiousHeavyRegression/gfg.txt". File already exists.
After running the above code, above error is thrown this is because the bool overwrite values used in the above code is false.
Similar Reads
File.Copy(String, String) Method in C# with Examples File.Copy(String, String) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file which is created by this function. Syntax:  public static void Copy (string sourceFileName, string destFileName); Parameter: This function acce
3 min read
File.Copy(String, String) Method in C# with Examples File.Copy(String, String) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file which is created by this function. Syntax:  public static void Copy (string sourceFileName, string destFileName); Parameter: This function acce
3 min read
File.Replace(String, String, String, Boolean) Method in C# with Examples File.Replace(String, String, String, Boolean) is an inbuilt File class method that is used to replace the contents of a specified destination file with the contents of a source file then it deletes the source file, creates a backup of the replaced file, and optionally ignores merge errors. Syntax: p
3 min read
File.Replace(String, String, String, Boolean) Method in C# with Examples File.Replace(String, String, String, Boolean) is an inbuilt File class method that is used to replace the contents of a specified destination file with the contents of a source file then it deletes the source file, creates a backup of the replaced file, and optionally ignores merge errors. Syntax: p
3 min read
File.AppendAllText(String, String) Method in C# with Examples File.AppendAllText(String, String) is an inbuilt File class method which is used to append the specified string to the given file if that file exists else creates a new file and then appending is done. It also closes the file.Syntax:Â Â public static void AppendAllText (string path, string contents);
3 min read
File.AppendAllText(String, String) Method in C# with Examples File.AppendAllText(String, String) is an inbuilt File class method which is used to append the specified string to the given file if that file exists else creates a new file and then appending is done. It also closes the file.Syntax:Â Â public static void AppendAllText (string path, string contents);
3 min read