DirectoryInfo.MoveTo(String) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
DirectoryInfo 인스턴스와 해당 내용을 새 경로로 이동합니다.
public:
void MoveTo(System::String ^ destDirName);
public void MoveTo(string destDirName);
member this.MoveTo : string -> unit
Public Sub MoveTo (destDirName As String)
매개 변수
- destDirName
- String
이 디렉터리를 이동할 이름 및 경로입니다. 대상은 다른 디스크 볼륨이나 이름이 동일한 디렉터리일 수 없습니다. 이 디렉터리를 하위 디렉터리로 추가하려는 기존 디렉터리일 수 있습니다.
예외
destDirName은 null입니다.
destDirName 은 빈 문자열(''")입니다.
디렉터리를 다른 볼륨으로 이동하려고 했습니다.
-또는-
destDirName 이미 존재합니다.
-또는-
이 경로에 액세스할 권한이 없습니다.
-또는-
이동 중인 디렉터리와 대상 디렉터리의 이름은 같습니다.
호출자에게 필요한 권한이 없습니다.
대상 디렉터리를 찾을 수 없습니다.
예제
다음 예제에서는 디렉터리를 이동하는 방법을 보여 줍니다.
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)
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("That directory does not exist.");
}
// Point the DirectoryInfo reference to the new directory.
//di = new DirectoryInfo("NewTempDir");
// Delete the directory.
//di.Delete(true);
}
}
open System.IO
// Make a reference to a directory.
let di = DirectoryInfo "TempDir"
// Create the directory only if it does not already exist.
if not di.Exists then
di.Create()
// Create a subdirectory in the directory just created.
let dis = di.CreateSubdirectory "SubDir"
// Move the main directory. Note that the contents move with the directory.
if not (Directory.Exists "NewTempDir") then
di.MoveTo "NewTempDir"
try
// Attempt to delete the subdirectory. Note that because it has been
// moved, an exception is thrown.
dis.Delete true
with _ ->
// 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
설명
예를 들어 c:\mydir를 IOException c:\public으로 이동하려고 하면 c:\public이 이미 있는 경우 이 메서드가 throw됩니다. "c:\\public\\\mydir"을 매개 변수로 destDirName 지정하거나 "c:\\newdir"과 같은 새 디렉터리 이름을 지정해야 합니다.
이 메서드를 사용하면 디렉터리를 읽기 전용 디렉터리로 이동할 수 있습니다. 두 디렉터리의 읽기/쓰기 특성은 영향을 받지 않습니다.
일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.