DirectoryInfo.Delete 메서드

정의

경로에서 DirectoryInfo 및 해당 내용을 삭제합니다.Deletes a DirectoryInfo and its contents from a path.

오버로드

Delete()

DirectoryInfo가 비어 있으면 이를 삭제합니다.Deletes this DirectoryInfo if it is empty.

Delete(Boolean)

하위 디렉터리와 파일을 삭제할지 여부를 지정하여 DirectoryInfo의 이 인스턴스를 삭제합니다.Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.

Delete()

DirectoryInfo가 비어 있으면 이를 삭제합니다.Deletes this DirectoryInfo if it is empty.

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

예외

디렉터리에 읽기 전용 파일이 포함되어 있습니다.The directory contains a read-only file.

DirectoryInfo 개체에서 설명하는 디렉터리가 없거나 찾을 수 없는 경우The directory described by this DirectoryInfo object does not exist or could not be found.

디렉터리가 비어 있지 않은 경우The directory is not empty.

또는-or- 이 디렉터리는 애플리케이션의 현재 작업 디렉터리입니다.The directory is the application's current working directory.

또는-or- 디렉터리에 대한 열린 핸들이 있고 운영 체제가 Windows XP 또는 그 이전 버전인 경우.There is an open handle on the directory, and the operating system is Windows XP or earlier. 이 열린 핸들은 디렉터리 열거로 인해 발생할 수 있습니다.This open handle can result from enumerating directories. 자세한 내용은 방법: 디렉터리 및 파일 열거를 참조하세요.For more information, see How to: Enumerate Directories and Files.

호출자에게 필요한 권한이 없는 경우The caller does not have the required permission.

예제

다음 예에서는 비어 있지 않은 디렉터리를 삭제 하려고 하면 예외가 throw 됩니다.The following example throws an exception if you attempt to delete a directory that is not empty.

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 {}
    }
}
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 작업을 참조 하세요.For a list of common I/O tasks, see Common I/O Tasks.

보안

FileIOPermission
디렉터리를 작성 합니다.for writing directories. 연결 된 열거형: WriteAssociated enumeration: Write

추가 정보

Delete(Boolean)

하위 디렉터리와 파일을 삭제할지 여부를 지정하여 DirectoryInfo의 이 인스턴스를 삭제합니다.Deletes this instance of a DirectoryInfo, specifying whether to delete subdirectories and files.

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입니다.true to delete this directory, its subdirectories, and all files; otherwise, false.

예외

디렉터리에 읽기 전용 파일이 포함되어 있습니다.The directory contains a read-only file.

DirectoryInfo 개체에서 설명하는 디렉터리가 없거나 찾을 수 없는 경우The directory described by this DirectoryInfo object does not exist or could not be found.

디렉터리가 읽기 전용인 경우The directory is read-only.

또는-or- 디렉터리에 하나 이상의 파일이나 하위 디렉터리가 있으며 recursivefalse인 경우The directory contains one or more files or subdirectories and recursive is false.

또는-or- 이 디렉터리는 애플리케이션의 현재 작업 디렉터리입니다.The directory is the application's current working directory.

또는-or- 디렉터리 또는 해당 파일 중 하나에 대한 열린 핸들이 있고 운영 체제가 Windows XP 또는 그 이전 버전인 경우.There is an open handle on the directory or on one of its files, and the operating system is Windows XP or earlier. 이 열린 핸들은 디렉터리 및 파일을 열거하면 발생할 수 있습니다.This open handle can result from enumerating directories and files. 자세한 내용은 방법: 디렉터리 및 파일 열거를 참조하세요.For more information, see How to: Enumerate Directories and Files.

호출자에게 필요한 권한이 없는 경우The caller does not have the required permission.

예제

다음 예제에서는 디렉터리를 삭제 하는 방법을 보여 줍니다.The following example demonstrates deleting a directory. 디렉터리가 제거 되었으므로 먼저 Delete 줄을 주석으로 처리 하 여 디렉터리가 있는지 테스트 합니다.Because the directory is removed, first comment out the Delete line to test that the directory exists. 그런 다음 동일한 디렉터리가 성공적으로 제거 되었는지 테스트 하는 코드 줄 주석 처리를 제거 합니다.Then uncomment the same line of code to test that the directory was removed successfully.

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);
    }
}
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에 파일이 나 하위 디렉터리가 없는 경우이 메서드는 recursive false되더라도 DirectoryInfo를 삭제 합니다.If the DirectoryInfo has no files or subdirectories, this method deletes the DirectoryInfo even if recursive is false. recursive false 때 비어 있지 않은 DirectoryInfo를 삭제 하려고 하면 IOExceptionthrow 됩니다.Attempting to delete a DirectoryInfo that is not empty when recursive is false throws an IOException.

일반적인 i/o 작업 목록은 일반적인 I/o 작업을 참조 하세요.For a list of common I/O tasks, see Common I/O Tasks.

보안

FileIOPermissionAccess
삭제할 DirectoryInfo 및 하위 디렉터리에 대 한 쓰기 권한이 필요한 쓰기 플래그입니다.Write flag required for write permission to the DirectoryInfo and subdirectories to be deleted.

추가 정보

적용 대상