File.Replace 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将指定文件的内容替换为另一个文件的内容、删除原始文件以及创建已替换文件的备份。
重载
Replace(String, String, String) |
将指定文件的内容替换为另一个文件的内容,删除原始文件并创建已替换文件的备份。 |
Replace(String, String, String, Boolean) |
将指定文件的内容替换为另一个文件的内容,删除原始文件并创建已替换文件的备份,并选择性地忽略合并错误。 |
注解
Replace 方法将指定文件的内容替换为另一个文件的内容。 它们还会创建已替换的文件的备份。
Replace(String, String, String)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
将指定文件的内容替换为另一个文件的内容,删除原始文件并创建已替换文件的备份。
public:
static void Replace(System::String ^ sourceFileName, System::String ^ destinationFileName, System::String ^ destinationBackupFileName);
public static void Replace (string sourceFileName, string destinationFileName, string? destinationBackupFileName);
public static void Replace (string sourceFileName, string destinationFileName, string destinationBackupFileName);
static member Replace : string * string * string -> unit
Public Shared Sub Replace (sourceFileName As String, destinationFileName As String, destinationBackupFileName As String)
参数
- sourceFileName
- String
替换由 destinationFileName
指定的文件的文件的名称。
- destinationFileName
- String
要替换的文件的名称。
- destinationBackupFileName
- String
备份文件的名称。
例外
destinationFileName
或 destinationBackupFileName
参数描述的路径不是法律形式。
destinationFileName
参数 null
。
指定了无效驱动器。
找不到 sourceFileName
或 destinationFileName
参数描述的文件。
指定的路径、文件名或两者都超过了系统定义的最大长度。
sourceFileName
或 destinationFileName
参数指定只读文件。
-或-
当前平台上不支持此操作。
-或-
源或目标参数指定目录而不是文件。
-或-
调用方没有所需的权限。
-或
sourceFileName
和 destinationFileName
指定相同的现有目录。
示例
下面的代码示例使用 Replace 方法将文件替换为另一个文件,并创建已替换文件的备份。
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)
{
File::Replace(fileToMoveAndDelete, fileToReplace,
backupOfFileToReplace, false);
}
int main()
{
try
{
String^ originalFile = "test.xml";
String^ fileToReplace = "test2.xml";
String^ backUpOfFileToReplace = "test2.xml.bac";
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");
}
catch (IOException^ ex)
{
Console::WriteLine(ex->Message);
}
};
using System;
using System.IO;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
try
{
string OriginalFile = "test.xml";
string FileToReplace = "test2.xml";
string BackUpOfFileToReplace = "test2.xml.bac";
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");
}
catch (Exception e)
{
Console.WriteLine(e);
}
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)
{
File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, false);
}
}
}
open System
open System.IO
// Move a file into another file, delete the original, and create a backup of the replaced file.
let replaceFile fileToMoveAndDelete fileToReplace backupOfFileToReplace =
File.Replace(fileToMoveAndDelete, fileToReplace, backupOfFileToReplace, false)
let originalFile = "test.xml"
let fileToReplace = "test2.xml"
let backUpOfFileToReplace = "test2.xml.bac"
printfn
$"Move the contents of {originalFile} into {fileToReplace}, delete {originalFile}, and create a backup of {fileToReplace}."
// Replace the file.
replaceFile originalFile fileToReplace backUpOfFileToReplace
printfn "Done"
Imports System.IO
Module FileExample
Sub Main()
Try
Dim OriginalFile As String = "test.xml"
Dim FileToReplace As String = "test2.xml"
Dim BackUpOfFileToReplace As String = "test2.xml.bac"
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")
Catch e As Exception
Console.WriteLine(e)
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)
' Replace the file.
File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, False)
End Sub
End Module
注解
Replace 方法将指定文件的内容替换为另一个文件的内容。 它还会创建已替换的文件的备份。
如果 sourceFileName
和 destinationFileName
位于不同的卷上,此方法将引发异常。 如果 destinationBackupFileName
位于与源文件不同的卷上,则会删除备份文件。
如果不想创建要替换的文件的备份,请将 null
传递给 destinationBackupFileName
参数。
如果 destinationBackupFileName
已存在,则会将其替换为 destinationFileName
文件的内容。
适用于
Replace(String, String, String, Boolean)
- Source:
- File.cs
- Source:
- File.cs
- Source:
- File.cs
将指定文件的内容替换为另一个文件的内容,删除原始文件并创建已替换文件的备份,并选择性地忽略合并错误。
public:
static void Replace(System::String ^ sourceFileName, System::String ^ destinationFileName, System::String ^ destinationBackupFileName, bool ignoreMetadataErrors);
public static void Replace (string sourceFileName, string destinationFileName, string? destinationBackupFileName, bool ignoreMetadataErrors);
public static void Replace (string sourceFileName, string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);
static member Replace : string * string * string * bool -> unit
Public Shared Sub Replace (sourceFileName As String, destinationFileName As String, destinationBackupFileName As String, ignoreMetadataErrors As Boolean)
参数
- sourceFileName
- String
替换由 destinationFileName
指定的文件的文件的名称。
- destinationFileName
- String
要替换的文件的名称。
- destinationBackupFileName
- String
备份文件的名称。
- ignoreMetadataErrors
- Boolean
true
忽略替换文件中的合并错误(如属性和访问控制列表(ACL)到替换文件;否则,false
。
例外
destinationFileName
或 destinationBackupFileName
参数描述的路径不是法律形式。
destinationFileName
参数 null
。
指定了无效驱动器。
找不到 sourceFileName
或 destinationFileName
参数描述的文件。
指定的路径、文件名或两者都超过了系统定义的最大长度。
sourceFileName
或 destinationFileName
参数指定只读文件。
-或-
当前平台上不支持此操作。
-或-
源或目标参数指定目录而不是文件。
-或-
调用方没有所需的权限。
-或
sourceFileName
和 destinationFileName
指定相同的现有目录。
示例
下面的代码示例使用 Replace 方法将文件替换为另一个文件,并创建已替换文件的备份。
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)
{
File::Replace(fileToMoveAndDelete, fileToReplace,
backupOfFileToReplace, false);
}
int main()
{
try
{
String^ originalFile = "test.xml";
String^ fileToReplace = "test2.xml";
String^ backUpOfFileToReplace = "test2.xml.bac";
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");
}
catch (IOException^ ex)
{
Console::WriteLine(ex->Message);
}
};
using System;
using System.IO;
namespace FileSystemExample
{
class FileExample
{
public static void Main()
{
try
{
string OriginalFile = "test.xml";
string FileToReplace = "test2.xml";
string BackUpOfFileToReplace = "test2.xml.bac";
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");
}
catch (Exception e)
{
Console.WriteLine(e);
}
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)
{
File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, false);
}
}
}
open System
open System.IO
// Move a file into another file, delete the original, and create a backup of the replaced file.
let replaceFile fileToMoveAndDelete fileToReplace backupOfFileToReplace =
File.Replace(fileToMoveAndDelete, fileToReplace, backupOfFileToReplace, false)
let originalFile = "test.xml"
let fileToReplace = "test2.xml"
let backUpOfFileToReplace = "test2.xml.bac"
printfn
$"Move the contents of {originalFile} into {fileToReplace}, delete {originalFile}, and create a backup of {fileToReplace}."
// Replace the file.
replaceFile originalFile fileToReplace backUpOfFileToReplace
printfn "Done"
Imports System.IO
Module FileExample
Sub Main()
Try
Dim OriginalFile As String = "test.xml"
Dim FileToReplace As String = "test2.xml"
Dim BackUpOfFileToReplace As String = "test2.xml.bac"
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")
Catch e As Exception
Console.WriteLine(e)
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)
' Replace the file.
File.Replace(FileToMoveAndDelete, FileToReplace, BackupOfFileToReplace, False)
End Sub
End Module
注解
Replace 方法将指定文件的内容替换为另一个文件的内容。 它还会创建已替换的文件的备份。
如果 sourceFileName
和 destinationFileName
位于不同的卷上,此方法将引发异常。 如果 destinationBackupFileName
位于与源文件不同的卷上,则会删除备份文件。
如果不想创建要替换的文件的备份,请将 null
传递给 destinationBackupFileName
参数。
如果 destinationBackupFileName
已存在,则会将其替换为 destinationFileName
文件的内容。