DirectoryInfo.Delete 메서드

정의

경로에서 DirectoryInfo 및 해당 내용을 삭제합니다.

오버로드

Delete()

DirectoryInfo가 비어 있으면 이를 삭제합니다.

Delete(Boolean)

하위 디렉터리와 파일을 삭제할지 여부를 지정하여 DirectoryInfo의 이 인스턴스를 삭제합니다.

Delete()

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

DirectoryInfo가 비어 있으면 이를 삭제합니다.

public:
 override void Delete();
public override void Delete ();
override this.Delete : unit -> unit
Public Overrides Sub Delete ()

예외

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

DirectoryInfo 개체에서 설명하는 디렉터리가 없거나 찾을 수 없는 경우

디렉터리가 비어 있지 않은 경우

또는

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

또는

디렉터리에 대한 열린 핸들이 있고 운영 체제가 Windows XP 또는 그 이전 버전인 경우. 이 열린 핸들은 디렉터리를 열거하면 발생할 수 있습니다. 자세한 내용은 방법: 디렉터리 및 파일 열거를 참조하세요.

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

예제

다음 예제에서는 비어 있지 않은 디렉터리를 삭제하려고 하면 예외를 throw합니다.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Specify the directories you want to manipulate.
   DirectoryInfo^ di1 = gcnew DirectoryInfo( "c:\\MyDir" );
   try
   {
      
      // Create the directories.
      di1->Create();
      di1->CreateSubdirectory( "temp" );
      
      //This operation will not be allowed because there are subdirectories.
      Console::WriteLine( "I am about to attempt to delete {0}", di1->Name );
      di1->Delete();
      Console::WriteLine( "The Delete operation was successful, which was unexpected." );
   }
   catch ( Exception^ ) 
   {
      Console::WriteLine( "The Delete operation failed as expected." );
   }

}
using System;
using System.IO;

class Test
{
    public static void Main()
    {
        // Specify the directories you want to manipulate.
        DirectoryInfo di1 = new DirectoryInfo(@"c:\MyDir");

        try
        {
            // Create the directories.
            di1.Create();
            di1.CreateSubdirectory("temp");

            //This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}", di1.Name);
            di1.Delete();
            Console.WriteLine("The Delete operation was successful, which was unexpected.");
        }
        catch (Exception)
        {
            Console.WriteLine("The Delete operation failed as expected.");
        }
        finally {}
    }
}
open System.IO

// Specify the directories you want to manipulate.
let di1 = DirectoryInfo @"c:\MyDir"

try
    // Create the directories.
    di1.Create()
    di1.CreateSubdirectory "temp" |> ignore

    //This operation will not be allowed because there are subdirectories.
    printfn $"I am about to attempt to delete {di1.Name}"
    di1.Delete()
    printfn "The Delete operation was successful, which was unexpected."
with _ ->
    printfn "The Delete operation failed as expected."
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim di1 As DirectoryInfo = New DirectoryInfo("c:\MyDir")

        Try
            ' Create the directories.
            di1.Create()
            di1.CreateSubdirectory("temp")

            'This operation will not be allowed because there are subdirectories.
            Console.WriteLine("I am about to attempt to delete {0}", di1.Name)
            di1.Delete()
            Console.WriteLine("The Delete operation was successful, which was unexpected.")

        Catch
            Console.WriteLine("The Delete operation was unsuccessful, as expected.")
        End Try
    End Sub
End Class

설명

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

추가 정보

적용 대상

Delete(Boolean)

Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs
Source:
DirectoryInfo.cs

하위 디렉터리와 파일을 삭제할지 여부를 지정하여 DirectoryInfo의 이 인스턴스를 삭제합니다.

public:
 void Delete(bool recursive);
public void Delete (bool recursive);
override this.Delete : bool -> unit
Public Sub Delete (recursive As Boolean)

매개 변수

recursive
Boolean

이 디렉터리, 하위 디렉터리 및 모든 파일을 삭제하려면true 이고, 그렇지 않으면 false입니다.

예외

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

DirectoryInfo 개체에서 설명하는 디렉터리가 없거나 찾을 수 없는 경우

디렉터리가 읽기 전용인 경우

또는

디렉터리에 하나 이상의 파일이나 하위 디렉터리가 있으며 recursivefalse인 경우

또는

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

또는

디렉터리 또는 해당 파일 중 하나에 대한 열린 핸들이 있고 운영 체제가 Windows XP 또는 그 이전 버전인 경우. 이 열린 핸들은 디렉터리 및 파일을 열거하면 발생할 수 있습니다. 자세한 내용은 방법: 디렉터리 및 파일 열거를 참조하세요.

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

예제

다음 예제에서는 디렉터리를 삭제하는 방법을 보여 줍니다. 디렉터리가 제거되었으므로 먼저 줄을 주석 Delete 처리하여 디렉터리가 있는지 테스트합니다. 그런 다음 동일한 코드 줄의 주석 처리를 제거하여 디렉터리가 성공적으로 제거되었는지 테스트합니다.

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" );
   
   // Process that directory as required.
   // ...
   // Delete the subdirectory. The true indicates that if subdirectories
   // or files are in this directory, they are to be deleted as well.
   dis->Delete( true );
   
   // Delete the directory.
   di->Delete( true );
}
using System;
using System.IO;

public class DeleteTest
{
    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");

        // Process that directory as required.
        // ...

        // Delete the subdirectory. The true indicates that if subdirectories
        // or files are in this directory, they are to be deleted as well.
        dis.Delete(true);

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

// Process that directory as required.
// ...

// Delete the subdirectory. The true indicates that if subdirectories
// or files are in this directory, they are to be deleted as well.
dis.Delete true

// Delete the directory.
di.Delete true
Imports System.IO

Public Class DeleteTest

    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

        Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")
        ' Create a subdirectory in the directory just created.

        ' Process that directory as required.
        ' ...

        ' Delete the subdirectory. The true indicates that if subdirectories
        ' or files are in this directory, they are to be deleted as well.
        dis.Delete(True)

        ' Delete the directory.
        di.Delete(True)
    End Sub
End Class

설명

DirectoryInfo 에 파일 또는 하위 디렉터리가 없는 경우 이 메서드는 가 인 경우에도 recursivefalseDirectoryInfo 삭제합니다. 가 false 을 throw할 때 recursive 비어 있지 않은 을 IOException삭제 DirectoryInfo 하려고 시도합니다.

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

추가 정보

적용 대상