DirectoryInfo.Delete 方法

定義

從路徑上刪除 a DirectoryInfo 及其內容。

多載

名稱 Description
Delete()

如果是空的,就會刪除。DirectoryInfo

Delete(Boolean)

刪除此實例 DirectoryInfo,指定是否刪除子目錄與檔案。

Delete()

來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs

如果是空的,就會刪除。DirectoryInfo

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

例外狀況

該目錄包含一個唯讀檔案。

這個 DirectoryInfo 物件所描述的目錄不存在或找不到。

目錄不是空的。

-或-

該目錄是應用程式目前的工作目錄。

-或-

目錄上有一個開放的帳號,作業系統為 Windows XP 或更早版本。 這種開放式帳號可能是因為列舉目錄而產生的。 欲了解更多資訊,請參閱 「如何列舉目錄與檔案」。

來電者沒有所需的權限。

範例

以下範例若你嘗試刪除非空目錄,會拋出例外。

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 任務的清單,請參見 Common I/O 任務

另請參閱

適用於

Delete(Boolean)

來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs
來源:
DirectoryInfo.cs
來源:
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

-或-

該目錄是應用程式目前的工作目錄。

-或-

目錄或其檔案上有一個開放的 handle,作業系統為 Windows XP 或更早版本。 這種開放帳號可能是因為列舉目錄和檔案而產生的。 欲了解更多資訊,請參閱 「如何列舉目錄與檔案」。

來電者沒有所需的權限。

範例

以下範例示範刪除目錄的過程。 因為目錄已被移除,先註解該 Delete 行以測試該目錄是否存在。 接著取消註解同一段程式碼,測試該目錄是否成功移除。

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)
            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 檔案或子目錄,此方法會刪除 DirectoryInfo 即使 recursivefalse。 嘗試刪除 DirectoryInfo 當 是 recursive 不空false的 時,會拋出一個 IOException

關於常見 I/O 任務的清單,請參見 Common I/O 任務

另請參閱

適用於