File.Replace(String, String, String) Method in C# with Examples
Last Updated :
28 Apr, 2025
File.Replace(String, String, String) 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 and creates a backup of the replaced file.
Syntax:
public static void Replace (string sourceFileName, string destinationFileName, string destinationBackupFileName);
Parameter: This function accepts three parameters which are illustrated below:
- sourceFileName: This is the specified source file.
- destinationFileName: This is the specified destination file whose contents get replaced with the contents of the source file.
- destinationBackupFileName: This file contains the backup of replaced destination file's content.
Exceptions:
- ArgumentException: The path described by the destinationFileName parameter was not of a legal form. OR the path described by the destinationBackupFileName parameter was not of a legal form.
- ArgumentNullException: The destinationFileName parameter is null.
- DriveNotFoundException: An invalid drive was specified.
- FileNotFoundException: The file described by the current FileInfo object could not be found. OR the file described by the destinationBackupFileName parameter could not be found.
- IOException: An I/O error occurred while opening the file. OR the sourceFileName and destinationFileName parameters specify the same file.
- PathTooLongException: The specified path, file name, or both exceed the system-defined maximum length.
- PlatformNotSupportedException: The operating system is Windows 98 Second Edition or earlier and the files system is not NTFS.
- UnauthorizedAccessException: The sourceFileName or destinationFileName parameter specifies a file that is read-only. OR this operation is not supported on the current platform. OR source or destination parameters specify a directory instead of a file. OR the caller does not have the required permission.
Below are the programs to illustrate the File.Replace(String, String, String) method.
Program 1: Before running the below code, three files have been created, where source file is file1.txt, destination file is file2.txt and backup file is file3.txt. These files' contents are shown below-
CSharp
// C# program to illustrate the usage
// of File.Replace(String, String, String) method
// Using System and System.IO namespaces
using System;
using System.IO;
class GFG {
public static void Main()
{
// Specifying 3 files
string sourceFileName = "file1.txt";
string destinationFileName = "file2.txt";
string destinationBackupFileName = "file3.txt";
// Calling the Replace() function
File.Replace(sourceFileName, destinationFileName,
destinationBackupFileName);
Console.WriteLine("Replacement process has been done.");
}
}
Output:
Replacement process has been done.
After running the above code, the above output is shown, the source file is deleted, and the remaining two file's contents are shown below-
Program 2: Before running the below code, two files have been created, where source file is file1.txt, destination file is file2.txt and there is no backup file because we do not want to keep backup of the replaced file. These files' contents are shown below-
C#
// C# program to illustrate the usage
// of File.Replace(String, String, String) method
// Using System and System.IO namespaces
using System;
using System.IO;
class GFG {
public static void Main()
{
// Specifying 2 files
string sourceFileName = "file1.txt";
string destinationFileName = "file2.txt";
// Calling the Replace() function with
// null parameter inplace of backup file because
// we do not want to keep backup of the
// replaced file.
File.Replace(sourceFileName, destinationFileName, null);
Console.WriteLine("Replacement process has been done.");
}
}
Output:
Replacement process has been done.
After running the above code, the above output is shown, the source file is deleted, and the destination file contents are shown below-
Similar Reads
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.ReadLines(String) Method in C# with Examples File.ReadLines(String) is an inbuilt File class method that is used to read the lines of a file. Syntax: public static System.Collections.Generic.IEnumerable ReadLines (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file for reading
2 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.WriteAllText(String, String) Method in C# with Examples File.WriteAllText(String, String) is an inbuilt File class method that is used to create a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten. Syntax: public static void WriteAllText (string path, string contents); Parame
3 min read
File.WriteAllLines(String, String[]) Method in C# with Examples File.WriteAllLines(String, String[]) is an inbuilt File class method that is used to create a new file, writes the specified string array to the file, and then closes the file.Syntax:Â Â public static void WriteAllLines (string path, string[] contents); Parameter: This function accepts two parameters
3 min read
File.ReadLines(String, Encoding) Method in C# with Examples File.ReadLines(String, Encoding) is an inbuilt File class method that is used to read the lines of a file that has a specified encoding. Syntax: public static System.Collections.Generic.IEnumerable ReadLines (string path, System.Text.Encoding encoding); Parameter: This function accepts two parameter
2 min read