FileInfo.Replace Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
A megadott fájl tartalmát lecseréli az aktuális FileInfo objektum által leírt fájlra, törli az eredeti fájlt, és biztonsági másolatot készít a lecserélt fájlról.
Túlterhelések
| Name | Description |
|---|---|
| Replace(String, String) |
A megadott fájl tartalmát lecseréli az aktuális FileInfo objektum által leírt fájlra, törli az eredeti fájlt, és biztonsági másolatot készít a lecserélt fájlról. |
| Replace(String, String, Boolean) |
A megadott fájl tartalmát lecseréli az aktuális FileInfo objektum által leírt fájlra, törli az eredeti fájlt, és biztonsági másolatot készít a lecserélt fájlról. Azt is megadja, hogy figyelmen kívül hagyja-e az egyesítési hibákat. |
Megjegyzések
Akkor használja a Replace metódusokat, ha gyorsan le kell cserélnie egy fájlt az aktuális FileInfo objektum által leírt fájl tartalmára.
Replace(String, String)
A megadott fájl tartalmát lecseréli az aktuális FileInfo objektum által leírt fájlra, törli az eredeti fájlt, és biztonsági másolatot készít a lecserélt fájlról.
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
Paraméterek
- destinationFileName
- String
Az aktuális fájlra lecserélendő fájl neve.
- destinationBackupFileName
- String
Annak a fájlnak a neve, amellyel biztonsági másolatot szeretne készíteni a destinationFileName paraméter által leírt fájlról.
Válaszok
Egy FileInfo objektum, amely a paraméter által destinationFileName leírt fájl adatait foglalja magában.
- Attribútumok
Kivételek
A paraméter által destinationFileName leírt elérési út nem jogi forma volt.
-vagy-
A paraméter által destinationBackupFileName leírt elérési út nem jogi forma volt.
A destinationFileName paraméter a következő null: .
Az aktuális FileInfo objektum által leírt fájl nem található.
-vagy-
A paraméter által destinationFileName leírt fájl nem található.
A jelenlegi operációs rendszer nem Microsoft Windows NT vagy újabb.
Példák
Az alábbi példa egy másik fájlra cseréli a Replace fájlt, és biztonsági másolatot készít a lecserélt fájlról.
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
Megjegyzések
A Replace metódus lecseréli egy adott fájl tartalmát az aktuális FileInfo objektum által leírt fájl tartalmára. Emellett létrehoz egy biztonsági másolatot a lecserélt fájlról. Végül egy új FileInfo objektumot ad vissza, amely leírja a felülírt fájlt.
Adja meg null a destinationBackupFileName paramétert, ha nem szeretne biztonsági másolatot készíteni a lecserélt fájlról.
A következőre érvényes:
Replace(String, String, Boolean)
A megadott fájl tartalmát lecseréli az aktuális FileInfo objektum által leírt fájlra, törli az eredeti fájlt, és biztonsági másolatot készít a lecserélt fájlról. Azt is megadja, hogy figyelmen kívül hagyja-e az egyesítési hibákat.
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
Paraméterek
- destinationFileName
- String
Az aktuális fájlra lecserélendő fájl neve.
- destinationBackupFileName
- String
Annak a fájlnak a neve, amellyel biztonsági másolatot szeretne készíteni a destinationFileName paraméter által leírt fájlról.
- ignoreMetadataErrors
- Boolean
true figyelmen kívül hagyja az egyesítési hibákat (például attribútumokat és ACL-eket) a lecserélt fájlból a cserefájlba; egyéb esetben false.
Válaszok
Egy FileInfo objektum, amely a paraméter által destinationFileName leírt fájl adatait foglalja magában.
- Attribútumok
Kivételek
A paraméter által destinationFileName leírt elérési út nem jogi forma volt.
-vagy-
A paraméter által destinationBackupFileName leírt elérési út nem jogi forma volt.
A destinationFileName paraméter a következő null: .
Az aktuális FileInfo objektum által leírt fájl nem található.
-vagy-
A paraméter által destinationFileName leírt fájl nem található.
A jelenlegi operációs rendszer nem Microsoft Windows NT vagy újabb.
Példák
Az alábbi példa egy másik fájlra cseréli a Replace fájlt, és biztonsági másolatot készít a lecserélt fájlról.
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
Megjegyzések
A Replace metódus lecseréli egy adott fájl tartalmát az aktuális FileInfo objektum által leírt fájl tartalmára. Emellett létrehoz egy biztonsági másolatot a lecserélt fájlról. Végül egy új FileInfo objektumot ad vissza, amely leírja a felülírt fájlt.
Adja meg null a destinationBackupFileName paramétert, ha nem szeretne biztonsági másolatot készíteni a lecserélt fájlról.