FileInfo.Replace 方法

定义

使用当前 FileInfo 对象所描述的文件替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。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.

重载

Replace(String, String)

使用当前 FileInfo 对象所描述的文件替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。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.

Replace(String, String, Boolean)

使用当前 FileInfo 对象所描述的文件替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。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. 还指定是否忽略合并错误。Also specifies whether to ignore merge errors.

注解

如果需要快速将文件替换为当前FileInfo对象所描述的文件的内容, 请使用方法。ReplaceUse the Replace methods when you need to quickly replace a file with the contents of the file described by the current FileInfo object.

Replace(String, String)

使用当前 FileInfo 对象所描述的文件替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。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.

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);
member this.Replace : string * string -> System.IO.FileInfo
Public Function Replace (destinationFileName As String, destinationBackupFileName As String) As FileInfo

参数

destinationFileName
String

要替换为当前文件的文件的名称。The name of a file to replace with the current file.

destinationBackupFileName
String

文件的名称,该文件用于创建 destFileName 参数所描述的文件的备份。The name of a file with which to create a backup of the file described by the destFileName parameter.

返回

一个 FileInfo 对象,该对象封装有关 destFileName 参数所描述的文件的信息。A FileInfo object that encapsulates information about the file described by the destFileName parameter.

属性

异常

destFileName 参数描述的路径格式不合法。The path described by the destFileName parameter was not of a legal form.

-or- destBackupFileName 参数描述的路径格式不合法。The path described by the destBackupFileName parameter was not of a legal form.

destFileName 参数为 nullThe destFileName parameter is null.

找不到当前 FileInfo 对象描述的文件。The file described by the current FileInfo object could not be found.

-or- 找不到 destinationFileName 参数描述的文件。The file described by the destinationFileName parameter could not be found.

当前操作系统不是 Microsoft Windows NT 或更高版本。The current operating system is not Microsoft Windows NT or later.

示例

下面的示例使用Replace方法将文件替换为其他文件, 并创建被替换文件的备份。The following example uses the Replace method to replace a file with another file and create a backup of the replaced file.


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

注解

方法Replace将指定文件的内容替换为当前FileInfo对象所描述的文件的内容。The Replace method replaces the contents of a specified file with the contents of the file described by the current FileInfo object. 它还创建被替换文件的备份。It also creates a backup of the file that was replaced. 最后, 它返回一个新FileInfo的对象, 该对象描述覆盖的文件。Finally, it returns a new FileInfo object that describes the overwritten file.

注意

如果是只读的destFileName , 并且不会引发异常, 则此方法在 Windows 2000 环境中将会成功。This method will succeed in Windows 2000 environments if the destFileName is read-only and will not raise an exception. 在尝试替换目标文件之前, 使用属性检查目标文件是否为只读。IsReadOnlyUse the IsReadOnly property to check if the destination file is read-only before attempting to replace it.

如果null不想destBackupFileName创建要替换的文件的备份, 请将传递给参数。Pass null to the destBackupFileName parameter if you do not want to create a backup of the file being replaced.

安全性

FileIOPermission
关联的枚举:WriteAssociated enumeration: Write 安全操作:手动.Security Action: Demand. 用于写入由destBackupFileName参数描述的文件的权限 (如果指定了一个)。For permission to write to file described by the destBackupFileName parameter if one is specified.

Replace(String, String, Boolean)

使用当前 FileInfo 对象所描述的文件替换指定文件的内容,这一过程将删除原始文件,并创建被替换文件的备份。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. 还指定是否忽略合并错误。Also specifies whether to ignore merge errors.

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);
member this.Replace : string * string * bool -> System.IO.FileInfo
Public Function Replace (destinationFileName As String, destinationBackupFileName As String, ignoreMetadataErrors As Boolean) As FileInfo

参数

destinationFileName
String

要替换为当前文件的文件的名称。The name of a file to replace with the current file.

destinationBackupFileName
String

文件的名称,该文件用于创建 destFileName 参数所描述的文件的备份。The name of a file with which to create a backup of the file described by the destFileName parameter.

ignoreMetadataErrors
Boolean

若要忽略从被替换文件到替换文件的合并错误(例如特性和 ACL),请设置为 true;否则设置为 falsetrue to ignore merge errors (such as attributes and ACLs) from the replaced file to the replacement file; otherwise false.

返回

一个 FileInfo 对象,该对象封装有关 destFileName 参数所描述的文件的信息。A FileInfo object that encapsulates information about the file described by the destFileName parameter.

属性

异常

destFileName 参数描述的路径格式不合法。The path described by the destFileName parameter was not of a legal form.

- 或 --or- destBackupFileName 参数描述的路径格式不合法。The path described by the destBackupFileName parameter was not of a legal form.

destFileName 参数为 nullThe destFileName parameter is null.

找不到当前 FileInfo 对象描述的文件。The file described by the current FileInfo object could not be found.

-or- 找不到 destinationFileName 参数描述的文件。The file described by the destinationFileName parameter could not be found.

当前操作系统不是 Microsoft Windows NT 或更高版本。The current operating system is not Microsoft Windows NT or later.

示例

下面的示例使用Replace方法将文件替换为其他文件, 并创建被替换文件的备份。The following example uses the Replace method to replace a file with another file and create a backup of the replaced file.


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

注解

方法Replace将指定文件的内容替换为当前FileInfo对象所描述的文件的内容。The Replace method replaces the contents of a specified file with the contents of the file described by the current FileInfo object. 它还创建被替换文件的备份。It also creates a backup of the file that was replaced. 最后, 它返回一个新FileInfo的对象, 该对象描述覆盖的文件。Finally, it returns a new FileInfo object that describes the overwritten file.

注意

如果是只读的destFileName , 并且不会引发异常, 则此方法在 Windows 2000 环境中将会成功。This method will succeed in Windows 2000 environments if the destFileName is read-only and will not raise an exception. 在尝试替换目标文件之前, 使用属性检查目标文件是否为只读。IsReadOnlyUse the IsReadOnly property to check if the destination file is read-only before attempting to replace it.

如果null不想destBackupFileName创建要替换的文件的备份, 请将传递给参数。Pass null to the destBackupFileName parameter if you do not want to create a backup of the file being replaced.

安全性

FileIOPermission
关联的枚举:WriteAssociated enumeration: Write 安全操作:手动.Security Action: Demand. 用于写入由destBackupFileName参数描述的文件的权限 (如果指定了一个)。For permission to write to file described by the destBackupFileName parameter if one is specified.

适用于