C# FileInfo Replace(String, String)

Description

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

Syntax

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


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

Parameters

FileInfo.Replace(String, String) 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.

Returns

FileInfo.Replace(String, String) 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 ww  w . j  a v a  2s  .  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);
    }
}




















Home »
  C# Tutorial »
    System.IO »




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