FileInfo.Replace Method

Definition

Replaces the contents of a specified file with the file described by the current FileInfo object, deleting the original file and creating a backup of the replaced file.

Overloads

Replace(String, String)

Replaces the contents of a specified file with the file described by the current FileInfo object, deleting the original file and creating a backup of the replaced file.

Replace(String, String, Boolean)

Replaces the contents of a specified file with the file described by the current FileInfo object, deleting the original file, and creating a backup of the replaced file. Also specifies whether to ignore merge errors.

Remarks

Use the Replace methods when you need to quickly replace a file with the contents of the file described by the current FileInfo object.

Replace(String, String)

Source:
FileInfo.cs
Source:
FileInfo.cs
Source:
FileInfo.cs

Replaces the contents of a specified file with the file described by the current FileInfo object, deleting the original file and creating a backup of the replaced file.

C#
public System.IO.FileInfo Replace(string destinationFileName, string? destinationBackupFileName);
C#
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName);

Parameters

destinationFileName
String

The name of a file to replace with the current file.

destinationBackupFileName
String

The name of a file with which to create a backup of the file described by the destinationFileName parameter.

Returns

A FileInfo object that encapsulates information about the file described by the destinationFileName parameter.

Attributes

Exceptions

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.

The destinationFileName parameter is null.

The file described by the current FileInfo object could not be found.

-or-

The file described by the destinationFileName parameter could not be found.

The current operating system is not Microsoft Windows NT or later.

Examples

The following example uses the Replace method to replace a file with another file and create a backup of the replaced file.

C#
using System;
using System.IO;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                // originalFile and fileToReplace must contain the path to files that already exist in the
                // file system. backUpOfFileToReplace is created during the execution of the Replace method.

                string originalFile  = "test.txt";
                string fileToReplace = "test2.txt";
                string backUpOfFileToReplace = "test2.txt.bak";

                if (File.Exists(originalFile) && (File.Exists(fileToReplace)))
                {
                    Console.WriteLine("Move the contents of " + originalFile + " into " + fileToReplace + ", delete "
                        + originalFile + ", and create a backup of " + fileToReplace + ".");

                    // Replace the file.
                    ReplaceFile(originalFile, fileToReplace, backUpOfFileToReplace);

                    Console.WriteLine("Done");
                }
                else
                {
                    Console.WriteLine("Either the file {0} or {1} doesn't " + "exist.", originalFile, fileToReplace);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();
        }

        // Move a file into another file, delete the original, and create a backup of the replaced file.
        public static void ReplaceFile(string fileToMoveAndDelete, string fileToReplace, string backupOfFileToReplace)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(fileToMoveAndDelete);

            // replace the file.
            fInfo.Replace(fileToReplace, backupOfFileToReplace, false);
        }
    }
}
//Move the contents of test.txt into test2.txt, delete test.txt, and
//create a backup of test2.txt.
//Done

Remarks

The Replace method replaces the contents of a specified file with the contents of the file described by the current FileInfo object. It also creates a backup of the file that was replaced. Finally, it returns a new FileInfo object that describes the overwritten file.

Pass null to the destinationBackupFileName parameter if you do not want to create a backup of the file being replaced.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Replace(String, String, Boolean)

Source:
FileInfo.cs
Source:
FileInfo.cs
Source:
FileInfo.cs

Replaces the contents of a specified file with the file described by the current FileInfo object, deleting the original file, and creating a backup of the replaced file. Also specifies whether to ignore merge errors.

C#
public System.IO.FileInfo Replace(string destinationFileName, string? destinationBackupFileName, bool ignoreMetadataErrors);
C#
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);

Parameters

destinationFileName
String

The name of a file to replace with the current file.

destinationBackupFileName
String

The name of a file with which to create a backup of the file described by the destinationFileName parameter.

ignoreMetadataErrors
Boolean

true to ignore merge errors (such as attributes and ACLs) from the replaced file to the replacement file; otherwise false.

Returns

A FileInfo object that encapsulates information about the file described by the destinationFileName parameter.

Attributes

Exceptions

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.

The destinationFileName parameter is null.

The file described by the current FileInfo object could not be found.

-or-

The file described by the destinationFileName parameter could not be found.

The current operating system is not Microsoft Windows NT or later.

Examples

The following example uses the Replace method to replace a file with another file and create a backup of the replaced file.

C#
using System;
using System.IO;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                // originalFile and fileToReplace must contain the path to files that already exist in the
                // file system. backUpOfFileToReplace is created during the execution of the Replace method.

                string originalFile  = "test.txt";
                string fileToReplace = "test2.txt";
                string backUpOfFileToReplace = "test2.txt.bak";

                if (File.Exists(originalFile) && (File.Exists(fileToReplace)))
                {
                    Console.WriteLine("Move the contents of " + originalFile + " into " + fileToReplace + ", delete "
                        + originalFile + ", and create a backup of " + fileToReplace + ".");

                    // Replace the file.
                    ReplaceFile(originalFile, fileToReplace, backUpOfFileToReplace);

                    Console.WriteLine("Done");
                }
                else
                {
                    Console.WriteLine("Either the file {0} or {1} doesn't " + "exist.", originalFile, fileToReplace);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            Console.ReadLine();
        }

        // Move a file into another file, delete the original, and create a backup of the replaced file.
        public static void ReplaceFile(string fileToMoveAndDelete, string fileToReplace, string backupOfFileToReplace)
        {
            // Create a new FileInfo object.
            FileInfo fInfo = new FileInfo(fileToMoveAndDelete);

            // replace the file.
            fInfo.Replace(fileToReplace, backupOfFileToReplace, false);
        }
    }
}
//Move the contents of test.txt into test2.txt, delete test.txt, and
//create a backup of test2.txt.
//Done

Remarks

The Replace method replaces the contents of a specified file with the contents of the file described by the current FileInfo object. It also creates a backup of the file that was replaced. Finally, it returns a new FileInfo object that describes the overwritten file.

Pass null to the destinationBackupFileName parameter if you do not want to create a backup of the file being replaced.

Applies to

.NET 10 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1