다음을 통해 공유


DirectoryInfo.MoveTo 메서드

DirectoryInfo 인스턴스 및 해당 내용을 새 경로로 이동합니다.

네임스페이스: System.IO
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
Public Sub MoveTo ( _
    destDirName As String _
)
‘사용 방법
Dim instance As DirectoryInfo
Dim destDirName As String

instance.MoveTo(destDirName)
public void MoveTo (
    string destDirName
)
public:
void MoveTo (
    String^ destDirName
)
public void MoveTo (
    String destDirName
)
public function MoveTo (
    destDirName : String
)

매개 변수

  • destDirName
    이 디렉터리를 이동할 곳의 이름과 경로입니다. 대상 디렉터리는 다른 디스크 볼륨이나 동일한 이름의 디렉터리가 될 수는 없지만, 이 디렉터리를 하위 디렉터리로 추가할 기존 디렉터리가 될 수는 있습니다.

예외

예외 형식 조건

ArgumentNullException

destDirName이 Null 참조(Visual Basic의 경우 Nothing)인 경우

- 또는 -

이동되는 디렉터리와 대상 디렉터리의 이름이 같은 경우

ArgumentException

destDirName이 빈 문자열("")인 경우

IOException

다른 볼륨으로 디렉터리를 이동하려고 했거나 destDirName이 이미 있는 경우

SecurityException

호출자에게 필요한 권한이 없는 경우

설명

예를 들어, c:\mydir를 c:\public으로 이동하려 했을 때 c:\public이 이미 있으면 이 메서드는 IOException을 throw합니다. "c:\\public\\mydir"를 destDirName 매개 변수로 지정하거나 "c:\\newdir" 같은 새 디렉터리 이름을 지정해야 합니다.

이 메서드를 사용하면 디렉터리를 읽기 전용 디렉터리로 이동할 수 있습니다. 이들 디렉터리의 읽기/쓰기 특성은 영향을 받지 않습니다.

다음 표에서는 일반적인 예 또는 관련된 I/O 작업의 예를 보여 줍니다.

수행 작업

참조 항목

텍스트 파일을 만듭니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에 씁니다.

방법: 파일에 텍스트 쓰기

텍스트 파일에서 읽습니다.

방법: 파일의 텍스트 읽기

디렉터리를 삭제합니다.

Directory.Delete

DirectoryInfo.Delete

디렉터리를 만듭니다.

CreateDirectory

Directory

하위 디렉터리를 만듭니다.

CreateSubdirectory

디렉터리의 파일을 참조하십시오.

Name

디렉터리의 하위 디렉터리를 참조하십시오.

GetDirectories

GetDirectories

디렉터리에 있는 모든 하위 디렉터리와 모든 파일을 참조하십시오.

GetFileSystemInfos

디렉터리의 크기를 찾습니다.

Directory

파일이 있는지 여부를 확인합니다.

Exists

디렉터리가 있는지 여부를 확인합니다.

Exists

예제

다음 예제에서는 디렉터리를 이동하는 방법을 보여 줍니다.

Imports System
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 'Main
End Class 'MoveToTest
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);
    }
}
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);
}
import System.*;
import System.IO.*;

public class MoveToTest
{
    public static void main(String[] args)
    {
        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.get_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 (System.Exception exp) {
            // 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);
    } //main
} //MoveToTest 
import System;
import System.IO;

public class MoveToTest {
    public static function Main() {

        // Make a reference to a directory.
        var di : DirectoryInfo = 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.
        var dis : DirectoryInfo = di.CreateSubdirectory("SubDir");

        // Move the main directory. Note that its contents move also.
        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, 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);
    }
}
MoveToTest.Main();

.NET Framework 보안

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

DirectoryInfo 클래스
DirectoryInfo 멤버
System.IO 네임스페이스

기타 리소스

파일 및 스트림 I/O
방법: 파일의 텍스트 읽기
방법: 파일에 텍스트 쓰기