This documentation is archived and is not being maintained.

FileInfo.Replace Method (String, String, Boolean)

Note: This method is new in the .NET Framework version 2.0.

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.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(false)] 
public FileInfo Replace (
	string destinationFileName,
	string destinationBackupFileName,
	bool ignoreMetadataErrors
)
/** @attribute ComVisibleAttribute(false) */ 
public FileInfo Replace (
	String destinationFileName, 
	String destinationBackupFileName, 
	boolean ignoreMetadataErrors
)
ComVisibleAttribute(false) 
public function Replace (
	destinationFileName : String, 
	destinationBackupFileName : String, 
	ignoreMetadataErrors : boolean
) : FileInfo

Parameters

destinationFileName

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

destinationBackupFileName

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

ignoreMetadataErrors

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

Return Value

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

Exception typeCondition

ArgumentException

The path described by the destFileName parameter was not of a legal form.

-or-

The path described by the destBackupFileName parameter was not of a legal form.

ArgumentNullException

The destFileName parameter is a null reference (Nothing in Visual Basic).

FileNotFoundException

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

-or-

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

PlatformNotSupportedException

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

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.

Caution noteCaution

This method will succeed in Windows 2000 environments if the destFileName is read-only and will not raise an exception. Use the IsReadOnly property to check if the destination file is read-only before attempting to replace it.

Pass a null reference (Nothing in Visual Basic) to the destBackupFileName parameter if you do not want to create a backup of the file being replaced.

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

using System;
using System.IO;

namespace FileSystemExample
{
    class FileExample
    {
        public static void Main()
        {
            try
            {
                string OriginalFile = "test.xml";
                string FileToReplace = "test2.xml";
                string BackUpOfFileToReplace = "test2.xml.bac";

                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");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            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);

        }
    }
}

  • FileIOPermission  Associated enumerations: Read , Write Security Action: Demand. For permission to read and write to the current file and the file described by the destFileName parameter.
  • FileIOPermission  Associated enumeration: Write Security Action: Demand. For permission to write to file described by the destBackupFileName parameter if one is specified.

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0
Show: