FileInfo.Replace 메서드

정의

지정된 파일의 내용을 현재 FileInfo 개체에서 설명하는 파일로 대체합니다. 이때 원본 파일을 삭제하고 대체된 파일의 백업을 만듭니다.

오버로드

Replace(String, String)

지정된 파일의 내용을 현재 FileInfo 개체에서 설명하는 파일로 대체합니다. 이때 원본 파일을 삭제하고 대체된 파일의 백업을 만듭니다.

Replace(String, String, Boolean)

지정된 파일의 내용을 현재 FileInfo 개체에서 설명하는 파일로 대체합니다. 이때 원본 파일을 삭제하고 대체된 파일의 백업을 만듭니다. 병합 오류를 무시할지 여부도 지정합니다.

설명

파일을 현재 FileInfo 개체에서 Replace 설명하는 파일의 내용으로 빠르게 바꿔야 하는 경우 메서드를 사용합니다.

Replace(String, String)

Source:
FileInfo.cs
Source:
FileInfo.cs
Source:
FileInfo.cs

지정된 파일의 내용을 현재 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)

Source:
FileInfo.cs
Source:
FileInfo.cs
Source:
FileInfo.cs

지정된 파일의 내용을 현재 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 합니다.

적용 대상