Directory.Delete 메서드

정의

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

오버로드

Name Description
Delete(String)

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

Delete(String, Boolean)

지정된 디렉터리를 삭제하고 지정된 경우 디렉터리의 하위 디렉터리 및 파일을 삭제합니다.

Delete(String)

Source:
Directory.cs
Source:
Directory.cs
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 디렉터리가 비어 있지 않습니다.

-또는-

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

-또는-

디렉터리가 다른 프로세스에서 사용되고 있습니다.

호출자에게 필요한 권한이 없습니다.

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

pathnull입니다.

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

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
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

true 디렉터리, 하위 디렉터리 및 파일을 path제거하려면 ;이고, false그렇지 않으면 .

예외

지정 path 한 이름과 위치가 같은 파일이 있습니다.

-또는-

지정한 path 디렉터리가 읽기 전용이거나 recursivefalsepath 빈 디렉터리가 아닌 경우

-또는-

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

-또는-

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

-또는-

디렉터리가 다른 프로세스에서 사용되고 있습니다.

호출자에게 필요한 권한이 없습니다.

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

pathnull입니다.

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

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 디렉터리를 삭제하지 못할 수 있습니다.

추가 정보

적용 대상