DirectoryInfo.Delete 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
从路径中删除 DirectoryInfo 及其内容。
重载
Delete() |
如果此 DirectoryInfo 为空则将其删除。 |
Delete(Boolean) |
删除 DirectoryInfo 的此实例,指定是否删除子目录和文件。 |
Delete()
- 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 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)
- 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 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
如果没有文件或子目录,则即使 recursive
为 false
,DirectoryInfo
此方法也会删除 。 尝试删除 DirectoryInfo
当 为 false
时recursive
不为空的 会IOException引发 。
有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。