C# FileInfo Replace(String, String, Boolean)

Description

FileInfo 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.

Syntax

FileInfo.Replace(String, String, Boolean) has the following syntax.


[ComVisibleAttribute(false)]/* ww w . j av  a  2  s.  c  om*/
public FileInfo Replace(
  string destinationFileName,
  string destinationBackupFileName,
  bool ignoreMetadataErrors
)

Parameters

FileInfo.Replace(String, String, Boolean) has the following 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.

Returns

FileInfo.Replace(String, String, Boolean) method returns A FileInfo object that encapsulates information about the file described by the destFileName parameter.

Example

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


using System;/*from   w  w w.  ja  v a  2  s  .  com*/
using System.IO;

class FileExample
{
    public static void Main()
    {
        string originalFile  = "test.txt";
        string fileToReplace = "test2.txt";
        string backUpOfFileToReplace = "test2.txt.bak";

        FileInfo fInfo = new FileInfo(originalFile);

        fInfo.Replace(fileToReplace, backUpOfFileToReplace, false);
    }
}




















Home »
  C# Tutorial »
    System.IO »




BinaryReader
BinaryWriter
Directory
DirectoryInfo
DriveInfo
File
FileInfo
FileStream
MemoryStream
Path
StreamReader
StreamWriter
StringReader
StringWriter