英語で読む

次の方法で共有


FileInfo.Replace メソッド

定義

指定したファイルの内容を、現在の FileInfo オブジェクトによって記述されたファイルに置き換え、元のファイルを削除し、置き換えられたファイルのバックアップを作成します。

オーバーロード

Replace(String, String)

指定したファイルの内容を、現在の FileInfo オブジェクトによって記述されたファイルに置き換え、元のファイルを削除し、置き換えられたファイルのバックアップを作成します。

Replace(String, String, Boolean)

指定したファイルの内容を、現在の FileInfo オブジェクトによって記述されたファイルに置き換え、元のファイルを削除し、置き換えられたファイルのバックアップを作成します。 また、マージ エラーを無視するかどうかを指定します。

注釈

Replace メソッドは、ファイルを現在の FileInfo オブジェクトで記述されているファイルの内容にすばやく置き換える必要がある場合に使用します。

Replace(String, String)

ソース:
FileInfo.cs
ソース:
FileInfo.cs
ソース:
FileInfo.cs

指定したファイルの内容を、現在の FileInfo オブジェクトによって記述されたファイルに置き換え、元のファイルを削除し、置き換えられたファイルのバックアップを作成します。

C#
public System.IO.FileInfo Replace (string destinationFileName, string? destinationBackupFileName);
C#
public System.IO.FileInfo Replace (string destinationFileName, string destinationBackupFileName);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public System.IO.FileInfo Replace (string destinationFileName, string destinationBackupFileName);

パラメーター

destinationFileName
String

現在のファイルに置き換えるファイルの名前。

destinationBackupFileName
String

destinationFileName パラメーターによって記述されたファイルのバックアップを作成するファイルの名前。

戻り値

destinationFileName パラメーターによって記述されたファイルに関する情報をカプセル化する FileInfo オブジェクト。

属性

例外

destinationFileName パラメーターによって記述されたパスは、有効な形式ではありません。

-又は-

destinationBackupFileName パラメーターによって記述されたパスは、有効な形式ではありません。

destinationFileName パラメーターは nullです。

現在の FileInfo オブジェクトによって記述されたファイルが見つかりませんでした。

-又は-

destinationFileName パラメーターによって記述されたファイルが見つかりませんでした。

現在のオペレーティング システムが Microsoft Windows NT 以降ではありません。

次の例では、Replace メソッドを使用してファイルを別のファイルに置き換え、置き換えられたファイルのバックアップを作成します。

C#
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 オブジェクトを返します。

置き換えられるファイルのバックアップを作成しない場合は、nulldestinationBackupFileName パラメーターに渡します。

適用対象

.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 オブジェクトによって記述されたファイルに置き換え、元のファイルを削除し、置き換えられたファイルのバックアップを作成します。 また、マージ エラーを無視するかどうかを指定します。

C#
public System.IO.FileInfo Replace (string destinationFileName, string? destinationBackupFileName, bool ignoreMetadataErrors);
C#
public System.IO.FileInfo Replace (string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);
C#
[System.Runtime.InteropServices.ComVisible(false)]
public System.IO.FileInfo Replace (string destinationFileName, string destinationBackupFileName, bool ignoreMetadataErrors);

パラメーター

destinationFileName
String

現在のファイルに置き換えるファイルの名前。

destinationBackupFileName
String

destinationFileName パラメーターによって記述されたファイルのバックアップを作成するファイルの名前。

ignoreMetadataErrors
Boolean

置換されたファイルから置換ファイルへのマージ エラー (属性や ACL など) を無視する true。それ以外の場合は false

戻り値

destinationFileName パラメーターによって記述されたファイルに関する情報をカプセル化する FileInfo オブジェクト。

属性

例外

destinationFileName パラメーターによって記述されたパスは、有効な形式ではありません。

-又は-

destinationBackupFileName パラメーターによって記述されたパスは、有効な形式ではありません。

destinationFileName パラメーターは nullです。

現在の FileInfo オブジェクトによって記述されたファイルが見つかりませんでした。

-又は-

destinationFileName パラメーターによって記述されたファイルが見つかりませんでした。

現在のオペレーティング システムが Microsoft Windows NT 以降ではありません。

次の例では、Replace メソッドを使用してファイルを別のファイルに置き換え、置き換えられたファイルのバックアップを作成します。

C#
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 オブジェクトを返します。

置き換えられるファイルのバックアップを作成しない場合は、nulldestinationBackupFileName パラメーターに渡します。

適用対象

.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