FileInfo.Replace メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定したファイルの内容を現在の FileInfo オブジェクトが示すファイルと置き換え、元のファイルを削除し、置き換えられたファイルのバックアップを作成します。
オーバーロード
Replace(String, String) |
指定したファイルの内容を現在の FileInfo オブジェクトが示すファイルと置き換え、元のファイルを削除し、置き換えられたファイルのバックアップを作成します。 |
Replace(String, String, Boolean) |
指定したファイルの内容を現在の FileInfo オブジェクトが示すファイルと置き換え、元のファイルを削除し、置き換えられたファイルのバックアップを作成します。 また、マージ エラーを無視するかどうかも指定します。 |
注釈
メソッドは、 Replace ファイルを現在 FileInfo のオブジェクトによって記述されたファイルの内容にすばやく置き換える必要がある場合に使用します。
Replace(String, String)
指定したファイルの内容を現在の FileInfo オブジェクトが示すファイルと置き換え、元のファイルを削除し、置き換えられたファイルのバックアップを作成します。
public:
System::IO::FileInfo ^ Replace(System::String ^ destinationFileName, System::String ^ destinationBackupFileName);
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);
member this.Replace : string * string -> System.IO.FileInfo
[<System.Runtime.InteropServices.ComVisible(false)>]
member this.Replace : string * string -> System.IO.FileInfo
Public Function Replace (destinationFileName As String, destinationBackupFileName As String) As FileInfo
パラメーター
- destinationFileName
- String
現在のファイルと置き換えるファイルの名前。
- destinationBackupFileName
- String
destFileName
パラメーターが示すファイルのバックアップを作成するために使用するファイルの名前。
戻り値
destFileName
パラメーターが示すファイルに関する情報をカプセル化する FileInfo オブジェクト。
- 属性
例外
destFileName
パラメーターが示すパスは正しい書式ではありませんでした。
- または -
destBackupFileName
パラメーターが示すパスは正しい書式ではありませんでした。
destFileName
パラメーターが null
です。
現在の FileInfo オブジェクトで記述されているファイルが見つかりませんでした。
- または -
destinationFileName
パラメーターで記述されているファイルが見つかりませんでした。
現在のオペレーティング システムは Microsoft Windows NT 以降ではありません。
例
次の例では、 メソッドを 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)
{
// 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 のオブジェクトによって記述されたファイルの内容に置き換えます。 また、置き換えられたファイルのバックアップも作成されます。 最後に、上書きされたファイルを記述する新しい FileInfo オブジェクトを返します。
置き換えられるファイルのdestBackupFileName
バックアップを作成しない場合は、 パラメーターに を渡null
します。
適用対象
Replace(String, String, Boolean)
指定したファイルの内容を現在の FileInfo オブジェクトが示すファイルと置き換え、元のファイルを削除し、置き換えられたファイルのバックアップを作成します。 また、マージ エラーを無視するかどうかも指定します。
public:
System::IO::FileInfo ^ Replace(System::String ^ destinationFileName, System::String ^ destinationBackupFileName, bool ignoreMetadataErrors);
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);
member this.Replace : string * string * bool -> System.IO.FileInfo
[<System.Runtime.InteropServices.ComVisible(false)>]
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
現在のファイルと置き換えるファイルの名前。
- destinationBackupFileName
- String
destFileName
パラメーターが示すファイルのバックアップを作成するために使用するファイルの名前。
- ignoreMetadataErrors
- Boolean
置き換えられるファイルから置き換えるファイルへのマージ エラー (属性や ACL など) を無視する場合は true
。それ以外の場合は false
。
戻り値
destFileName
パラメーターが示すファイルに関する情報をカプセル化する FileInfo オブジェクト。
- 属性
例外
destFileName
パラメーターが示すパスは正しい書式ではありませんでした。
- または -
destBackupFileName
パラメーターが示すパスは正しい書式ではありませんでした。
destFileName
パラメーターが null
です。
現在の FileInfo オブジェクトで記述されているファイルが見つかりませんでした。
- または -
destinationFileName
パラメーターで記述されているファイルが見つかりませんでした。
現在のオペレーティング システムは Microsoft Windows NT 以降ではありません。
例
次の例では、 メソッドを 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)
{
// 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 のオブジェクトによって記述されたファイルの内容に置き換えます。 また、置き換えられたファイルのバックアップも作成されます。 最後に、上書きされたファイルを記述する新しい FileInfo オブジェクトを返します。
置き換えられるファイルのdestBackupFileName
バックアップを作成しない場合は、 パラメーターに を渡null
します。