Directory.Delete 메서드

정의

지정한 디렉터리와 선택적으로 하위 디렉터리를 삭제합니다.

오버로드

Delete(String)

지정된 경로에서 빈 디렉터리를 삭제합니다.

Delete(String, Boolean)

지정된 디렉터리와 해당 디렉터리의 하위 디렉터리 및 파일을 삭제합니다.

Delete(String)

Source:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

지정된 경로에서 빈 디렉터리를 삭제합니다.

public:
 static void Delete(System::String ^ path);
public static void Delete (string path);
static member Delete : string -> unit
Public Shared Sub Delete (path As String)

매개 변수

path
String

제거할 빈 디렉터리의 이름입니다. 해당 디렉터리는 쓰기가 가능하고 비어 있어야 합니다.

예외

path에 지정된 이름 및 위치와 동일한 파일이 있습니다.

또는

이 디렉터리는 애플리케이션의 현재 작업 디렉터리입니다.

또는

path에 지정된 디렉터리가 비어 있지 않습니다.

또는

디렉터리가 읽기 전용이거나 읽기 전용 파일을 포함합니다.

또는

디렉터리가 다른 프로세스에서 사용 중입니다.

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

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

path이(가) null인 경우

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

path가 존재하지 않거나 찾을 수 없습니다.

또는

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

예제

다음 예제에서는 새 디렉터리 및 하위 디렉터리를 만든 다음 하위 디렉터리만 삭제하는 방법을 보여줍니다.

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string subPath = @"C:\NewDirectory\NewSubDirectory";

            try
            {
                Directory.CreateDirectory(subPath);
                Directory.Delete(subPath);

                bool directoryExists = Directory.Exists(@"C:\NewDirectory");
                bool subDirectoryExists = Directory.Exists(subPath);

                Console.WriteLine("top-level directory exists: " + directoryExists);
                Console.WriteLine("sub-directory exists: " + subDirectoryExists);
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.Message);
            }
        }
    }
}
open System.IO

let subPath = @"C:\NewDirectory\NewSubDirectory"

try
    Directory.CreateDirectory subPath |> ignore
    Directory.Delete subPath

    let directoryExists = Directory.Exists @"C:\NewDirectory"
    let subDirectoryExists = Directory.Exists subPath

    printfn $"top-level directory exists: {directoryExists}"
    printfn $"sub-directory exists: {subDirectoryExists}"
with e ->
    printfn $"The process failed: {e.Message}"
Imports System.IO

Module Module1

    Sub Main()
        Dim subPath = "C:\NewDirectory\NewSubDirectory"

        Try
            Directory.CreateDirectory(subPath)
            Directory.Delete(subPath)

            Dim directoryExists = Directory.Exists("C:\NewDirectory")
            Dim subDirectoryExists = Directory.Exists(subPath)

            Console.WriteLine("top-level directory exists: " & directoryExists)
            Console.WriteLine("sub-directory exists: " & subDirectoryExists)

        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.Message)
        End Try
    End Sub

End Module

설명

이 메서드는 두 번째 매개 변수에 Delete(String, Boolean) 대해 지정된 와 false 동일하게 동작합니다.

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

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

이 메서드는 IOException 매개 변수에 지정된 path 디렉터리에 파일 또는 하위 디렉터리가 포함된 경우 을 throw합니다.

매개 변수의 path 대/소문자 구분은 코드가 실행 중인 파일 시스템의 대/소문자 구분에 해당합니다. 예를 들어 NTFS(기본 Windows 파일 시스템)에서는 대/소문자를 구분하지 않으며 Linux 파일 시스템에서는 대/소문자를 구분합니다.

경우에 따라 지정된 디렉터리가 파일 탐색기 열려 있는 경우 메서드가 Delete 디렉터리를 삭제하지 못할 수 있습니다.

추가 정보

적용 대상

Delete(String, Boolean)

Source:
Directory.cs
Source:
Directory.cs
Source:
Directory.cs

지정된 디렉터리와 해당 디렉터리의 하위 디렉터리 및 파일을 삭제합니다.

public:
 static void Delete(System::String ^ path, bool recursive);
public static void Delete (string path, bool recursive);
static member Delete : string * bool -> unit
Public Shared Sub Delete (path As String, recursive As Boolean)

매개 변수

path
String

제거할 디렉터리의 이름입니다.

recursive
Boolean

path의 디렉터리, 하위 디렉터리 및 파일을 제거하려면 true이고, 제거하지 않으려면 false입니다.

예외

path에 지정된 이름 및 위치와 동일한 파일이 있습니다.

또는

path로 지정한 디렉터리가 읽기 전용이거나 recursivefalse이고 path가 빈 디렉터리가 아닙니다.

또는

이 디렉터리는 애플리케이션의 현재 작업 디렉터리입니다.

또는

디렉터리에 읽기 전용 파일이 포함되어 있습니다.

또는

디렉터리가 다른 프로세스에서 사용 중입니다.

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

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

path이(가) null인 경우

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

path가 존재하지 않거나 찾을 수 없습니다.

또는

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

예제

다음 예제에서는 하위 디렉터리에 새 디렉터리, 하위 디렉터리 및 파일을 만든 다음 모든 새 항목을 재귀적으로 삭제하는 방법을 보여 줍니다.

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string topPath = @"C:\NewDirectory";
            string subPath = @"C:\NewDirectory\NewSubDirectory";

            try
            {
                Directory.CreateDirectory(subPath);

                using (StreamWriter writer = File.CreateText(subPath + @"\example.txt"))
                {
                    writer.WriteLine("content added");
                }

                Directory.Delete(topPath, true);

                bool directoryExists = Directory.Exists(topPath);

                Console.WriteLine("top-level directory exists: " + directoryExists);
            }
            catch (Exception e)
            {
                Console.WriteLine("The process failed: {0}", e.Message);
            }
        }
    }
}
open System.IO

let topPath = @"C:\NewDirectory"
let subPath = @"C:\NewDirectory\NewSubDirectory"

try
    Directory.CreateDirectory(subPath) |> ignore

    do
        use writer = File.CreateText(subPath + @"\example.txt")
        writer.WriteLine "content added"

    Directory.Delete(topPath, true)

    let directoryExists = Directory.Exists topPath

    printfn $"top-level directory exists: {directoryExists}"
    
with e ->
    printfn $"The process failed: {e.Message}"
Imports System.IO

Module Module1

    Sub Main()
        Dim topPath = "C:\NewDirectory"
        Dim subPath = "C:\NewDirectory\NewSubDirectory"

        Try
            Directory.CreateDirectory(subPath)

            Using writer As StreamWriter = File.CreateText(subPath + "\example.txt")
                writer.WriteLine("content added")
            End Using

            Directory.Delete(topPath, True)

            Dim directoryExists = Directory.Exists(topPath)

            Console.WriteLine("top-level directory exists: " & directoryExists)
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.Message)
        End Try
    End Sub

End Module

설명

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

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

매개 변수의 path 대/소문자 구분은 코드가 실행 중인 파일 시스템의 대/소문자 구분에 해당합니다. 예를 들어 NTFS(기본 Windows 파일 시스템)에서는 대/소문자를 구분하지 않으며 Linux 파일 시스템에서는 대/소문자를 구분합니다.

매개 변수가 인 recursivetrue경우 사용자는 현재 디렉터리 및 모든 하위 디렉터리에 대한 쓰기 권한이 있어야 합니다.

이 메서드의 동작은 기호 링크 또는 탑재 지점과 같은 재분석 지점이 포함된 디렉터리를 삭제할 때 약간 다릅니다. 재분석 지점이 탑재 지점과 같은 디렉터리인 경우 탑재 지점이 분리되고 탑재 지점이 삭제됩니다. 이 메서드는 재분석 지점을 통해 재귀하지 않습니다. 재분석 지점이 파일에 대한 기호 링크인 경우 재분석 지점은 심볼 링크의 대상이 아니라 삭제됩니다.

경우에 따라 지정된 디렉터리가 파일 탐색기 열려 있는 경우 메서드가 Delete 디렉터리를 삭제하지 못할 수 있습니다.

추가 정보

적용 대상