Export (0) Print
Expand All

File.Replace Method (String, String, String, Boolean)

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

Replaces the contents of a specified file with the contents of another file, deleting the original file, and creating a backup of the replaced file and optionally ignores merge errors.

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

'Declaration
Public Shared Sub Replace ( _
	sourceFileName As String, _
	destinationFileName As String, _
	destinationBackupFileName As String, _
	ignoreMetadataErrors As Boolean _
)
'Usage
Dim sourceFileName As String
Dim destinationFileName As String
Dim destinationBackupFileName As String
Dim ignoreMetadataErrors As Boolean

File.Replace(sourceFileName, destinationFileName, destinationBackupFileName, ignoreMetadataErrors)
public static void Replace (
	String sourceFileName, 
	String destinationFileName, 
	String destinationBackupFileName, 
	boolean ignoreMetadataErrors
)
public static function Replace (
	sourceFileName : String, 
	destinationFileName : String, 
	destinationBackupFileName : String, 
	ignoreMetadataErrors : boolean
)

Parameters

sourceFileName

The name of a file that replaces the file specified by destinationFileName.

destinationFileName

The name of the file being replaced.

destinationBackupFileName

The name of the backup file.

ignoreMetadataErrors

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

Exception typeCondition

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 a null reference (Nothing in Visual Basic).

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. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

PlatformNotSupportedException

The operating system is Windows 98 Second Edition or earlier and the files system is not NTFS.

UnauthorizedAccessException

The soureFileName 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.

The Replace method replaces the contents of a specified file with the contents of another file. It also creates a backup of the file that was replaced.

If the sourceFileName and destinationFileName are on different volumes, this method will raise an exception. If the destinationBackupFileName is on a different volume from the source file, the backup file will be deleted.

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

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

Imports System
Imports System.IO

Module FileExample

    Sub Main()
        Try
            Dim OriginalFile As String = "test.xml"
            Dim FileToReplace As String = "test2.xml"
            Dim BackUpOfFileToReplace As String = "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 e As Exception
            Console.WriteLine(e)
        End Try

        Console.ReadLine()

    End Sub

    ' Move a file into another file, delete the original, and create a backup of the replaced file.
    Sub ReplaceFile(ByVal FileToMoveAndDelete As String, ByVal FileToReplace As String, ByVal BackupOfFileToReplace As String)

        ' Replace the file.
        File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, False)

    End Sub
End Module

  • FileIOPermission  for permission to read and write to the file described by the sourceFileName parameter. Associated enumeration: Read Security action: Demand.
  • FileIOPermission  for permission to read and write to the file described by the destinationFileName parameter. Associated enumerations: Read , Write Security action: Demand.
  • FileIOPermission  for permission to write to file described by the destinationBackupFileName parameter if one is specified. Associated enumeration: Write Security action: Demand.

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

Community Additions

ADD
Show:
© 2014 Microsoft