Directory.Move(String, String) 메서드

정의

파일 또는 디렉터리와 그 내용을 새 위치로 이동합니다.

public:
 static void Move(System::String ^ sourceDirName, System::String ^ destDirName);
public static void Move (string sourceDirName, string destDirName);
static member Move : string * string -> unit
Public Shared Sub Move (sourceDirName As String, destDirName As String)

매개 변수

sourceDirName
String

이동할 파일 또는 디렉터리의 경로입니다.

destDirName
String

새 위치 또는 해당 콘텐츠에 대한 sourceDirName 경로입니다. sourceDirName이 파일이면 destDirName도 파일 이름이어야 합니다.

예외

디렉터리를 다른 볼륨으로 이동하려고 했습니다.

또는 destDirName 이 이미 있습니다. 주의 섹션의 메모를 참조하세요.

또는 sourceDirNamedestDirName 매개 변수가 동일한 파일 또는 디렉터리를 참조합니다.

또는 디렉터리 또는 포함된 파일이 다른 프로세스에서 사용 중입니다.

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

2.1보다 오래된 .NET Framework 및 .NET Core 버전: sourceDirName 또는 destDirName 길이가 0인 문자열이거나, 공백만 포함하거나, 하나 이상의 잘못된 문자를 포함합니다. GetInvalidPathChars() 메서드를 사용하여 잘못된 문자를 쿼리할 수 있습니다.

sourceDirName 또는 destDirNamenull인 경우

지정된 경로, 파일 이름 또는 둘 다가 시스템에서 정의한 최대 길이를 초과합니다.

sourceDirName에 의해 지정된 경로가 잘못되었습니다(예: 매핑되지 않은 드라이브에 있음).

예제

다음 예제에서는 디렉터리와 모든 해당 파일을 새 디렉터리로 이동하는 방법을 보여 줍니다. 원래 디렉터리가 이동된 후에는 더 이상 존재하지 않습니다.

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceDirectory = @"C:\source";
            string destinationDirectory = @"C:\destination";

            try
            {
                Directory.Move(sourceDirectory, destinationDirectory);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
open System.IO

let sourceDirectory = @"C:\source"
let destinationDirectory = @"C:\destination"

try
    Directory.Move(sourceDirectory, destinationDirectory)
with e ->
    printfn $"{e.Message}"
Imports System.IO

Module Module1

    Sub Main()
        Dim sourceDirectory As String = "C:\source"
        Dim destinationDirectory As String = "C:\destination"

        Try
            Directory.Move(sourceDirectory, destinationDirectory)
        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try

    End Sub

End Module

설명

이 메서드는 지정된 destDirName 이름으로 새 디렉터리를 만들고 파일 및 디렉터리를 포함한 내용을 sourceDirName새로 만든 대상 디렉터리로 이동합니다. 그런 다음 디렉터리를 삭제합니다 sourceDirName .

디렉터리를 이미 있는 IOException 디렉터리로 이동하려고 하면 발생합니다.

sourceDirNamedestDirName 인수는 상대 경로 또는 절대 경로 정보를 지정할 수 있습니다. 상대 경로 정보는 현재 작업 디렉터리를 기준으로 해석됩니다. 현재 작업 디렉터리를 가져오려면 다음을 참조하세요 GetCurrentDirectory.

후행 공백은 디렉터리를 이동하기 전에 경로 매개 변수의 끝에서 제거됩니다.

일반적인 I/O 작업 목록은 일반적인 I/O 작업을 참조하세요.

참고

.NET Core 3.0부터 메서드는 Move 이미 있는 경우 모든 플랫폼에서 destDirName throw IOException 합니다. .NET Core 2.2 및 이전 버전에서 예외는 Windows에서만 throw되었으며 다른 플랫폼은 실패하거나 덮어쓸 destDirName수 있습니다. C++ 이름 바꾸기를 참조하세요.

적용 대상

추가 정보