FileInfo.Replace 方法

定義

將指定檔案的內容取代為目前 FileInfo 物件所描述的檔案、刪除源檔,以及建立已取代檔案的備份。

多載

Replace(String, String)

將指定檔案的內容取代為目前 FileInfo 物件所描述的檔案、刪除源檔,以及建立已取代檔案的備份。

Replace(String, String, Boolean)

將指定檔案的內容取代為目前 FileInfo 物件所描述的檔案、刪除源檔,以及建立已取代檔案的備份。 也指定是否忽略合併錯誤。

備註

當您需要以目前 FileInfo 物件所描述的檔案內容快速取代檔案時,請使用 Replace 方法。

Replace(String, String)

來源:
FileInfo.cs
來源:
FileInfo.cs
來源:
FileInfo.cs

將指定檔案的內容取代為目前 FileInfo 物件所描述的檔案、刪除源檔,以及建立已取代檔案的備份。

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);

參數

destinationFileName
String

要取代為目前檔案的檔名。

destinationBackupFileName
String

用來建立 destinationFileName 參數所描述之檔案備份的檔名。

傳回

FileInfo 物件,封裝 destinationFileName 參數所描述之檔案的相關信息。

屬性

例外狀況

destinationFileName 參數所描述的路徑不是合法的形式。

-或-

destinationBackupFileName 參數所描述的路徑不是合法的形式。

destinationFileName 參數是 null

找不到目前 FileInfo 物件所描述的檔案。

-或-

找不到 destinationFileName 參數所描述的檔案。

目前的操作系統不是Microsoft Windows NT 或更新版本。

範例

下列範例使用 Replace 方法,將檔案取代為另一個檔案,並建立已取代檔案的備份。

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

備註

Replace 方法會將指定檔案的內容取代為目前 FileInfo 物件所描述的檔案內容。 它也會建立已取代之檔案的備份。 最後,它會傳回描述覆寫檔案的新 FileInfo 物件。

如果您不想建立要取代之檔案的備份,請將 null 傳遞至 destinationBackupFileName 參數。

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Replace(String, String, Boolean)

來源:
FileInfo.cs
來源:
FileInfo.cs
來源:
FileInfo.cs

將指定檔案的內容取代為目前 FileInfo 物件所描述的檔案、刪除源檔,以及建立已取代檔案的備份。 也指定是否忽略合併錯誤。

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);

參數

destinationFileName
String

要取代為目前檔案的檔名。

destinationBackupFileName
String

用來建立 destinationFileName 參數所描述之檔案備份的檔名。

ignoreMetadataErrors
Boolean

true 忽略從已取代檔案到取代檔案的合併錯誤(例如屬性和 ACL) ;否則 false

傳回

FileInfo 物件,封裝 destinationFileName 參數所描述之檔案的相關信息。

屬性

例外狀況

destinationFileName 參數所描述的路徑不是合法的形式。

-或-

destinationBackupFileName 參數所描述的路徑不是合法的形式。

destinationFileName 參數是 null

找不到目前 FileInfo 物件所描述的檔案。

-或-

找不到 destinationFileName 參數所描述的檔案。

目前的操作系統不是Microsoft Windows NT 或更新版本。

範例

下列範例使用 Replace 方法,將檔案取代為另一個檔案,並建立已取代檔案的備份。

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

備註

Replace 方法會將指定檔案的內容取代為目前 FileInfo 物件所描述的檔案內容。 它也會建立已取代之檔案的備份。 最後,它會傳回描述覆寫檔案的新 FileInfo 物件。

如果您不想建立要取代之檔案的備份,請將 null 傳遞至 destinationBackupFileName 參數。

適用於

.NET 9 及其他版本
產品 版本
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1