FileInfo.Replace Methode

Definition

Ersetzt den Inhalt einer angegebenen Datei durch die vom aktuellen FileInfo-Objekt beschriebene Datei, löscht die ursprüngliche Datei und erstellt eine Sicherungskopie der ersetzten Datei.

Überlädt

Replace(String, String)

Ersetzt den Inhalt einer angegebenen Datei durch die vom aktuellen FileInfo-Objekt beschriebene Datei, löscht die ursprüngliche Datei und erstellt eine Sicherungskopie der ersetzten Datei.

Replace(String, String, Boolean)

Ersetzt den Inhalt einer angegebenen Datei durch die vom aktuellen FileInfo-Objekt beschriebene Datei, löscht die ursprüngliche Datei und erstellt eine Sicherungskopie der ersetzten Datei. Gibt auch an, ob Mergefehler ignoriert werden sollen.

Hinweise

Verwenden Sie die Replace Methoden, wenn Sie eine Datei schnell durch den Inhalt der Datei ersetzen müssen, die vom aktuellen FileInfo Objekt beschrieben wird.

Replace(String, String)

Quelle:
FileInfo.cs
Quelle:
FileInfo.cs
Quelle:
FileInfo.cs

Ersetzt den Inhalt einer angegebenen Datei durch die vom aktuellen FileInfo-Objekt beschriebene Datei, löscht die ursprüngliche Datei und erstellt eine Sicherungskopie der ersetzten Datei.

public:
 System::IO::FileInfo ^ Replace(System::String ^ destinationFileName, System::String ^ destinationBackupFileName);
public System.IO.FileInfo Replace (string destinationFileName, string? destinationBackupFileName);
public System.IO.FileInfo Replace (string destinationFileName, string destinationBackupFileName);
[System.Runtime.InteropServices.ComVisible(false)]
public System.IO.FileInfo Replace (string destinationFileName, string destinationBackupFileName);
member this.Replace : string * string -> System.IO.FileInfo
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Replace : string * string -> System.IO.FileInfo
Public Function Replace (destinationFileName As String, destinationBackupFileName As String) As FileInfo

Parameter

destinationFileName
String

Der Name einer Datei, die durch die aktuelle Datei ersetzt werden soll.

destinationBackupFileName
String

Der Name einer Datei, mit der eine Sicherungskopie der mit dem destFileName-Parameter beschriebenen Datei erstellt werden soll.

Gibt zurück

Ein FileInfo-Objekt, das Informationen zu der mit dem destFileName-Parameter beschriebenen Datei kapselt.

Attribute

Ausnahmen

Der durch den destFileName-Parameter beschriebene Pfad hat ein ungültiges Format.

- oder -

Der durch den destBackupFileName-Parameter beschriebene Pfad hat ein ungültiges Format.

Der destFileName-Parameter ist null.

Die durch das aktuelle FileInfo-Objekt beschriebene Datei konnte nicht gefunden werden.

- oder -

Die durch den destinationFileName-Parameter beschriebene Datei konnte nicht gefunden werden.

Das aktuelle Betriebssystem ist nicht Microsoft Windows NT oder höher.

Beispiele

Im folgenden Beispiel wird die Replace -Methode verwendet, um eine Datei durch eine andere Datei zu ersetzen und eine Sicherung der ersetzten Datei zu erstellen.


using namespace System;
using namespace System::IO;


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

    // replace the file.
    fInfo->Replace(fileToReplace, backupOfFileToReplace, false);
}


int 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.xml";
        String^ fileToReplace = "test2.xml";
        String^ backUpOfFileToReplace = "test2.xml.bak";

        if (File::Exists(originalFile) && (File::Exists(fileToReplace)))
        {
            Console::WriteLine("Move the contents of {0} into {1}, " +
                "delete {0}, and create a backup of {1}",
                originalFile, 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 (IOException^ ex)
    {
        Console::WriteLine(ex->Message);
    }
}


//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.:
//
//Move the contents of c:\test1.xml into c:\test2.xml, delete c:\test1.xml, 
//and create a backup of c:\test2.xml
//Done
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
Imports System.IO

Module FileExample

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

            Dim originalFile As String = "test.xml"
            Dim fileToReplace As String = "test2.xml"
            Dim backUpOfFileToReplace As String = "test2.xml.bak"

            If (File.Exists(originalFile) And (File.Exists(fileToReplace))) Then
                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)
            End If
        Catch e As Exception
            Console.WriteLine(e.Message)
        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)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(fileToMoveAndDelete)

        ' Replace the file.
        fInfo.Replace(fileToReplace, backupOfFileToReplace, False)

    End Sub
End Module

' Move the contents of test.txt into test2.txt, delete test.txt, and 
' create a backup of test2.txt.
' Done

Hinweise

Die Replace -Methode ersetzt den Inhalt einer angegebenen Datei durch den Inhalt der Datei, die vom aktuellen FileInfo Objekt beschrieben wird. Außerdem wird eine Sicherung der ersetzten Datei erstellt. Schließlich wird ein neues FileInfo Objekt zurückgegeben, das die überschriebene Datei beschreibt.

Übergeben Sie null an den destBackupFileName Parameter, wenn Sie keine Sicherung der zu ersetzenden Datei erstellen möchten.

Gilt für:

Replace(String, String, Boolean)

Quelle:
FileInfo.cs
Quelle:
FileInfo.cs
Quelle:
FileInfo.cs

Ersetzt den Inhalt einer angegebenen Datei durch die vom aktuellen FileInfo-Objekt beschriebene Datei, löscht die ursprüngliche Datei und erstellt eine Sicherungskopie der ersetzten Datei. Gibt auch an, ob Mergefehler ignoriert werden sollen.

public:
 System::IO::FileInfo ^ Replace(System::String ^ destinationFileName, System::String ^ destinationBackupFileName, bool ignoreMetadataErrors);
public System.IO.FileInfo Replace (string destinationFileName, string? destinationBackupFileName, bool ignoreMetadataErrors);
public System.IO.FileInfo Replace (string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);
[System.Runtime.InteropServices.ComVisible(false)]
public System.IO.FileInfo Replace (string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);
member this.Replace : string * string * bool -> System.IO.FileInfo
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Replace : string * string * bool -> System.IO.FileInfo
Public Function Replace (destinationFileName As String, destinationBackupFileName As String, ignoreMetadataErrors As Boolean) As FileInfo

Parameter

destinationFileName
String

Der Name einer Datei, die durch die aktuelle Datei ersetzt werden soll.

destinationBackupFileName
String

Der Name einer Datei, mit der eine Sicherungskopie der mit dem destFileName-Parameter beschriebenen Datei erstellt werden soll.

ignoreMetadataErrors
Boolean

true, um Fehler beim Zusammenführen der ersetzten Datei und der Ersetzungsdatei zu ignorieren (z. B. Zugriffssteuerungslisten), andernfalls false.

Gibt zurück

Ein FileInfo-Objekt, das Informationen zu der mit dem destFileName-Parameter beschriebenen Datei kapselt.

Attribute

Ausnahmen

Der durch den destFileName-Parameter beschriebene Pfad hat ein ungültiges Format.

- oder -

Der durch den destBackupFileName-Parameter beschriebene Pfad hat ein ungültiges Format.

Der destFileName-Parameter ist null.

Die durch das aktuelle FileInfo-Objekt beschriebene Datei konnte nicht gefunden werden.

- oder -

Die durch den destinationFileName-Parameter beschriebene Datei konnte nicht gefunden werden.

Das aktuelle Betriebssystem ist nicht Microsoft Windows NT oder höher.

Beispiele

Im folgenden Beispiel wird die Replace -Methode verwendet, um eine Datei durch eine andere Datei zu ersetzen und eine Sicherung der ersetzten Datei zu erstellen.


using namespace System;
using namespace System::IO;


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

    // replace the file.
    fInfo->Replace(fileToReplace, backupOfFileToReplace, false);
}


int 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.xml";
        String^ fileToReplace = "test2.xml";
        String^ backUpOfFileToReplace = "test2.xml.bak";

        if (File::Exists(originalFile) && (File::Exists(fileToReplace)))
        {
            Console::WriteLine("Move the contents of {0} into {1}, " +
                "delete {0}, and create a backup of {1}",
                originalFile, 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 (IOException^ ex)
    {
        Console::WriteLine(ex->Message);
    }
}


//This code produces output similar to the following; 
//results may vary based on the computer/file structure/etc.:
//
//Move the contents of c:\test1.xml into c:\test2.xml, delete c:\test1.xml, 
//and create a backup of c:\test2.xml
//Done
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
Imports System.IO

Module FileExample

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

            Dim originalFile As String = "test.xml"
            Dim fileToReplace As String = "test2.xml"
            Dim backUpOfFileToReplace As String = "test2.xml.bak"

            If (File.Exists(originalFile) And (File.Exists(fileToReplace))) Then
                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)
            End If
        Catch e As Exception
            Console.WriteLine(e.Message)
        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)
        ' Create a new FileInfo object.
        Dim fInfo As New FileInfo(fileToMoveAndDelete)

        ' Replace the file.
        fInfo.Replace(fileToReplace, backupOfFileToReplace, False)

    End Sub
End Module

' Move the contents of test.txt into test2.txt, delete test.txt, and 
' create a backup of test2.txt.
' Done

Hinweise

Die Replace -Methode ersetzt den Inhalt einer angegebenen Datei durch den Inhalt der Datei, die vom aktuellen FileInfo Objekt beschrieben wird. Außerdem wird eine Sicherung der ersetzten Datei erstellt. Schließlich wird ein neues FileInfo Objekt zurückgegeben, das die überschriebene Datei beschreibt.

Übergeben Sie null an den destBackupFileName Parameter, wenn Sie keine Sicherung der zu ersetzenden Datei erstellen möchten.

Gilt für: