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.

範例

如果您嘗試刪除不是空的目錄,下列範例會擲回例外狀況。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,否則為 falsetrue 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- 這個目錄包含一或多個檔案或子目錄,且 recursivefalseThe 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,這個方法也會刪除 DirectoryInfoIf the DirectoryInfo has no files or subdirectories, this method deletes the DirectoryInfo even if recursive is false. recursive false 時,嘗試刪除不是空的 DirectoryInfo 會擲回 IOExceptionAttempting 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.

另請參閱

適用於