DirectoryInfo.MoveTo(String) 메서드

정의

DirectoryInfo 인스턴스 및 해당 내용을 새 경로로 이동합니다.Moves a DirectoryInfo instance and its contents to a new path.

public:
 void MoveTo(System::String ^ destDirName);
public void MoveTo (string destDirName);
member this.MoveTo : string -> unit
Public Sub MoveTo (destDirName As String)

매개 변수

destDirName
String

이 디렉터리를 이동할 곳의 이름과 경로입니다.The name and path to which to move this directory. 대상 디렉터리는 다른 디스크 볼륨이나 동일한 이름의 디렉터리가 될 수는 없지만,The destination cannot be another disk volume or a directory with the identical name. 이 디렉터리를 하위 디렉터리로 추가할 기존 디렉터리가 될 수는 있습니다.It can be an existing directory to which you want to add this directory as a subdirectory.

예외

destDirName이(가) null인 경우destDirName is null.

destDirName 이 빈 문자열(''")입니다.destDirName is an empty string (''").

디렉터리를 다른 볼륨으로 이동하려고 했습니다.An attempt was made to move a directory to a different volume.

또는-or- destDirName 이 이미 있습니다.destDirName already exists.

또는-or- 이 경로에 액세스할 권한이 없는 경우You are not authorized to access this path.

또는-or- 이동되는 디렉터리와 대상 디렉터리의 이름이 같은 경우The directory being moved and the destination directory have the same name.

호출자에게 필요한 권한이 없는 경우The caller does not have the required permission.

대상 디렉터리를 찾을 수 없는 경우The destination directory cannot be found.

예제

다음 예에서는 디렉터리를 이동 하는 방법을 보여 줍니다.The following example demonstrates moving a directory.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Make a reference to a directory.
   DirectoryInfo^ di = gcnew DirectoryInfo( "TempDir" );
   
   // Create the directory only if it does not already exist.
   if (  !di->Exists )
      di->Create();

   
   // Create a subdirectory in the directory just created.
   DirectoryInfo^ dis = di->CreateSubdirectory( "SubDir" );
   
   // Move the main directory. Note that the contents move with the directory.
   if (  !Directory::Exists( "NewTempDir" ) )
      di->MoveTo( "NewTempDir" );

   try
   {
      
      // Attempt to delete the subdirectory. Note that because it has been
      // moved, an exception is thrown.
      dis->Delete( true );
   }
   catch ( Exception^ ) 
   {
      
      // Handle this exception in some way, such as with the following code:
      // Console::WriteLine(S"That directory does not exist.");
   }

   
   // Point the DirectoryInfo reference to the new directory.
   //di = new DirectoryInfo(S"NewTempDir");
   // Delete the directory.
   //di->Delete(true);
}

using System;
using System.IO;

public class MoveToTest
{
    public static void Main()
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // Move the main directory. Note that the contents move with the directory.
        if (Directory.Exists("NewTempDir") == false)
            di.MoveTo("NewTempDir");

        try
        {
            // Attempt to delete the subdirectory. Note that because it has been
            // moved, an exception is thrown.
            dis.Delete(true);
        }
        catch (Exception)
        {
            // Handle this exception in some way, such as with the following code:
            // Console.WriteLine("That directory does not exist.");
        }

        // Point the DirectoryInfo reference to the new directory.
        //di = new DirectoryInfo("NewTempDir");

        // Delete the directory.
        //di.Delete(true);
    }
}
Imports System.IO

Public Class MoveToTest

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("TempDir")
        ' Create the directory only if it does not already exist.
        If di.Exists = False Then
            di.Create()
        End If

        ' Create a subdirectory in the directory just created.
        Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")
        If Directory.Exists("NewTempDir") = False Then
            ' Move the main directory. Note that the contents move with the directory.
            di.MoveTo("NewTempDir")
        End If
        Try
            ' Attempt to delete the subdirectory. Note that because it has been
            ' moved, an exception is thrown.
            dis.Delete(True)
        Catch
            ' Handle this exception in some way, such as with the following code:
            ' Console.WriteLine("That directory does not exist.");
            ' Point the DirectoryInfo reference to the new directory.
            ' di = New DirectoryInfo("NewTempDir")
            ' Delete the directory.
            ' di.Delete(True)        
        End Try

    End Sub
End Class

설명

예를 들어 IOException c:\mydir을 c:\public으로 이동 하려고 하 고 c:\public이 이미 있는 경우이 메서드는을 throw 합니다.This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. "C: \ \public \ \mydir" destDirName 를 매개 변수로 지정 하거나 "c: \newdir"과 같은 새 디렉터리 이름을 지정 해야 합니다 \ .You must specify "c:\\public\\mydir" as the destDirName parameter, or specify a new directory name such as "c:\\newdir".

이 메서드는 디렉터리를 읽기 전용 디렉터리로 이동할 수 있도록 합니다.This method permits moving a directory to a read-only directory. 두 디렉터리의 읽기/쓰기 특성에는 영향을 주지 않습니다.The read/write attribute of neither directory is affected.

일반적인 i/o 작업 목록은 일반적인 I/o 작업을 참조 하세요.For a list of common I/O tasks, see Common I/O Tasks.

적용 대상

추가 정보