FileInfo.Replace Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Ersetzt den Inhalt einer angegebenen Datei durch die vom aktuellen FileInfo Objekt beschriebene Datei, das Löschen der Originaldatei und das Erstellen einer Sicherung der ersetzten Datei.
Überlädt
| Name | Beschreibung |
|---|---|
| Replace(String, String) |
Ersetzt den Inhalt einer angegebenen Datei durch die vom aktuellen FileInfo Objekt beschriebene Datei, das Löschen der Originaldatei und das Erstellen einer Sicherung der ersetzten Datei. |
| Replace(String, String, Boolean) |
Ersetzt den Inhalt einer angegebenen Datei durch die vom aktuellen FileInfo Objekt beschriebene Datei, das Löschen der Originaldatei und das Erstellen einer Sicherung der ersetzten Datei. Gibt außerdem an, ob Zusammenführungsfehler ignoriert werden sollen. |
Hinweise
Verwenden Sie die Replace Methoden, wenn Sie eine Datei schnell durch den Inhalt der vom aktuellen FileInfo Objekt beschriebenen Datei ersetzen müssen.
Replace(String, String)
Ersetzt den Inhalt einer angegebenen Datei durch die vom aktuellen FileInfo Objekt beschriebene Datei, das Löschen der Originaldatei und das Erstellen einer Sicherung der ersetzten Datei.
public:
System::IO::FileInfo ^ Replace(System::String ^ destinationFileName, System::String ^ destinationBackupFileName);
[System.Runtime.InteropServices.ComVisible(false)]
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName);
public System.IO.FileInfo Replace(string destinationFileName, string destinationBackupFileName);
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Replace : string * string -> System.IO.FileInfo
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 Sicherung der durch den destinationFileName Parameter beschriebenen Datei erstellt werden soll.
Gibt zurück
Ein FileInfo Objekt, das Informationen über die vom destinationFileName Parameter beschriebene Datei kapselt.
- Attribute
Ausnahmen
Der vom destinationFileName Parameter beschriebene Pfad war keine Rechtsform.
-oder-
Der vom destinationBackupFileName Parameter beschriebene Pfad war keine Rechtsform.
Der destinationFileName Parameter ist null.
Die vom aktuellen FileInfo Objekt beschriebene Datei wurde nicht gefunden.
-oder-
Die vom destinationFileName Parameter beschriebene Datei wurde nicht gefunden.
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 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 vom aktuellen FileInfo Objekt beschriebenen Datei. 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 den destinationBackupFileName Parameter, wenn Sie keine Sicherung der zu ersetzenden Datei erstellen möchten.
Gilt für:
Replace(String, String, Boolean)
Ersetzt den Inhalt einer angegebenen Datei durch die vom aktuellen FileInfo Objekt beschriebene Datei, das Löschen der Originaldatei und das Erstellen einer Sicherung der ersetzten Datei. Gibt außerdem an, ob Zusammenführungsfehler ignoriert werden sollen.
public:
System::IO::FileInfo ^ Replace(System::String ^ destinationFileName, System::String ^ destinationBackupFileName, bool ignoreMetadataErrors);
[System.Runtime.InteropServices.ComVisible(false)]
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)>]
member this.Replace : string * string * bool -> System.IO.FileInfo
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 Sicherung der durch den destinationFileName Parameter beschriebenen Datei erstellt werden soll.
- ignoreMetadataErrors
- Boolean
true um Zusammenführungsfehler (z. B. Attribute und ACLs) von der ersetzten Datei an die Ersetzungsdatei zu ignorieren; andernfalls false.
Gibt zurück
Ein FileInfo Objekt, das Informationen über die vom destinationFileName Parameter beschriebene Datei kapselt.
- Attribute
Ausnahmen
Der vom destinationFileName Parameter beschriebene Pfad war keine Rechtsform.
-oder-
Der vom destinationBackupFileName Parameter beschriebene Pfad war keine Rechtsform.
Der destinationFileName Parameter ist null.
Die vom aktuellen FileInfo Objekt beschriebene Datei wurde nicht gefunden.
-oder-
Die vom destinationFileName Parameter beschriebene Datei wurde nicht gefunden.
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 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 vom aktuellen FileInfo Objekt beschriebenen Datei. 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 den destinationBackupFileName Parameter, wenn Sie keine Sicherung der zu ersetzenden Datei erstellen möchten.