DirectoryInfo.Delete 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從路徑刪除 DirectoryInfo 和其內容。
多載
Delete() |
如果這個 DirectoryInfo 是空的,則將它刪除。 |
Delete(Boolean) |
刪除 DirectoryInfo 的這個執行個體,並指定是否刪除子目錄和檔案。 |
Delete()
如果這個 DirectoryInfo 是空的,則將它刪除。
public:
override void Delete();
public override void Delete ();
override this.Delete : unit -> unit
Public Overrides Sub Delete ()
例外狀況
目錄包含唯讀檔案。
這個 DirectoryInfo 物件描述的目錄不存在,或找不到該目錄。
目錄不是空的。
-或-
目錄是應用程式目前的工作目錄。
-或-
此目錄具有開啟控制代碼,而且作業系統是 Windows XP 或先前的版本。 這個開啟控制代碼可能是列舉目錄所產生的結果。 如需詳細資訊,請參閱如何:列舉目錄和檔案。
呼叫端沒有必要的權限。
範例
如果您嘗試刪除不是空的目錄,下列範例會擲回例外狀況。
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)
刪除 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 物件描述的目錄不存在,或找不到該目錄。
目錄是唯讀的。
-或-
目錄包含一或多個檔案或子目錄,並且 recursive
是 false
。
-或-
目錄是應用程式目前的工作目錄。
-或-
此目錄或其中一個檔案具有開啟控制代碼,而且作業系統是 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
如果 沒有檔案或子目錄,則即使 為 false
,這個方法也會DirectoryInfo
刪除 。recursive
嘗試刪除 DirectoryInfo
擲回 時recursive
false
不是空的 IOException。
如需一般 I/O 工作的清單,請參閱 一般 I/O 工作。