DirectoryInfo.Delete 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从路径中删除 a DirectoryInfo 及其内容。
重载
| 名称 | 说明 |
|---|---|
| Delete() |
如果为 DirectoryInfo 空,则将其删除。 |
| Delete(Boolean) |
删除此实例 DirectoryInfo,指定是否删除子目录和文件。 |
Delete()
- Source:
- DirectoryInfo.cs
- Source:
- DirectoryInfo.cs
- 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或更早版本。 此打开的句柄可能会导致枚举目录。 有关详细信息,请参阅 “如何:枚举目录和文件”。
调用方没有所需的权限。
示例
如果尝试删除不为空的目录,以下示例将引发异常。
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
- 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 对象描述的目录不存在或找不到。
目录是只读的。
-或-
该目录包含一个或多个文件或子目录,并且 recursive 是 false。
-或-
目录是应用程序的当前工作目录。
-或-
目录或其中一个文件上有一个打开的句柄,操作系统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删除即使recursive为 。false 尝试删除引发时DirectoryInforecursive不为空的 。falseIOException
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。