Directory.Delete 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
删除指定的目录,并选择性地删除任何子目录。
重载
| 名称 | 说明 |
|---|---|
| Delete(String) |
从指定路径中删除空目录。 |
| Delete(String, Boolean) |
删除指定的目录,如果指示,则删除目录中的任何子目录和文件。 |
Delete(String)
从指定路径中删除空目录。
public:
static void Delete(System::String ^ path);
public static void Delete(string path);
static member Delete : string -> unit
Public Shared Sub Delete (path As String)
参数
- path
- String
要删除的空目录的名称。 此目录必须可写且为空。
例外
存在同名和位置 path 的文件。
-或-
目录是应用程序的当前工作目录。
-或-
指定的 path 目录不为空。
-或-
目录为只读目录,或包含只读文件。
-或-
目录正由另一个进程使用。
调用方没有所需的权限。
低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用该方法 GetInvalidPathChars() 查询无效字符。
path 是 null。
指定的路径、文件名或两者都超过了系统定义的最大长度。
示例
以下示例演示如何创建新的目录和子目录,然后仅删除子目录。
using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string subPath = @"C:\NewDirectory\NewSubDirectory";
try
{
Directory.CreateDirectory(subPath);
Directory.Delete(subPath);
bool directoryExists = Directory.Exists(@"C:\NewDirectory");
bool subDirectoryExists = Directory.Exists(subPath);
Console.WriteLine("top-level directory exists: " + directoryExists);
Console.WriteLine("sub-directory exists: " + subDirectoryExists);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.Message);
}
}
}
}
open System.IO
let subPath = @"C:\NewDirectory\NewSubDirectory"
try
Directory.CreateDirectory subPath |> ignore
Directory.Delete subPath
let directoryExists = Directory.Exists @"C:\NewDirectory"
let subDirectoryExists = Directory.Exists subPath
printfn $"top-level directory exists: {directoryExists}"
printfn $"sub-directory exists: {subDirectoryExists}"
with e ->
printfn $"The process failed: {e.Message}"
Imports System.IO
Module Module1
Sub Main()
Dim subPath = "C:\NewDirectory\NewSubDirectory"
Try
Directory.CreateDirectory(subPath)
Directory.Delete(subPath)
Dim directoryExists = Directory.Exists("C:\NewDirectory")
Dim subDirectoryExists = Directory.Exists(subPath)
Console.WriteLine("top-level directory exists: " & directoryExists)
Console.WriteLine("sub-directory exists: " & subDirectoryExists)
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.Message)
End Try
End Sub
End Module
注解
此方法的行为与Delete(String, Boolean)false为第二个参数指定的相同。
该 path 参数可以指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
删除目录之前, path 将从参数末尾删除尾随空格。
此方法引发 IOException 参数中指定的 path 目录是否包含文件或子目录。
参数的 path 区分大小写对应于运行代码的文件系统。 例如,它在 NTFS(默认 Windows 文件系统)上不区分大小写,在 Linux 文件系统上区分大小写。
在某些情况下,如果在文件资源管理器中打开了指定的目录,则 Delete 该方法可能无法删除它。
另请参阅
适用于
Delete(String, Boolean)
删除指定的目录,如果指示,则删除目录中的任何子目录和文件。
public:
static void Delete(System::String ^ path, bool recursive);
public static void Delete(string path, bool recursive);
static member Delete : string * bool -> unit
Public Shared Sub Delete (path As String, recursive As Boolean)
参数
- path
- String
要删除的目录的名称。
- recursive
- Boolean
true 如果删除目录、子目录和文件,则 path为 ;否则为 false。
例外
存在同名和位置 path 的文件。
-或-
指定的 path 目录是只读的,或者 recursivefalsepath 不是空目录。
-或-
目录是应用程序的当前工作目录。
-或-
目录包含只读文件。
-或-
目录正由另一个进程使用。
调用方没有所需的权限。
低于 2.1 的 .NET Framework 和 .NET Core 版本: path 是长度为零的字符串,仅包含空格,或包含一个或多个无效字符。 可以使用该方法 GetInvalidPathChars() 查询无效字符。
path 是 null。
指定的路径、文件名或两者都超过了系统定义的最大长度。
示例
以下示例演示如何在子目录中创建新的目录、子目录和文件,然后以递归方式删除所有新项。
using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
string topPath = @"C:\NewDirectory";
string subPath = @"C:\NewDirectory\NewSubDirectory";
try
{
Directory.CreateDirectory(subPath);
using (StreamWriter writer = File.CreateText(subPath + @"\example.txt"))
{
writer.WriteLine("content added");
}
Directory.Delete(topPath, true);
bool directoryExists = Directory.Exists(topPath);
Console.WriteLine("top-level directory exists: " + directoryExists);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.Message);
}
}
}
}
open System.IO
let topPath = @"C:\NewDirectory"
let subPath = @"C:\NewDirectory\NewSubDirectory"
try
Directory.CreateDirectory(subPath) |> ignore
do
use writer = File.CreateText(subPath + @"\example.txt")
writer.WriteLine "content added"
Directory.Delete(topPath, true)
let directoryExists = Directory.Exists topPath
printfn $"top-level directory exists: {directoryExists}"
with e ->
printfn $"The process failed: {e.Message}"
Imports System.IO
Module Module1
Sub Main()
Dim topPath = "C:\NewDirectory"
Dim subPath = "C:\NewDirectory\NewSubDirectory"
Try
Directory.CreateDirectory(subPath)
Using writer As StreamWriter = File.CreateText(subPath + "\example.txt")
writer.WriteLine("content added")
End Using
Directory.Delete(topPath, True)
Dim directoryExists = Directory.Exists(topPath)
Console.WriteLine("top-level directory exists: " & directoryExists)
Catch e As Exception
Console.WriteLine("The process failed: {0}", e.Message)
End Try
End Sub
End Module
注解
该 path 参数可以指定相对路径或绝对路径信息。 相对路径信息解释为相对于当前工作目录。 若要获取当前工作目录,请参阅 GetCurrentDirectory。
删除目录之前, path 将从参数末尾删除尾随空格。
参数的 path 区分大小写对应于运行代码的文件系统。 例如,它在 NTFS(默认 Windows 文件系统)上不区分大小写,在 Linux 文件系统上区分大小写。
recursive如果参数为true,则用户必须具有当前目录以及所有子目录的写入权限。
删除包含重新分析点的目录(如符号链接或装入点)时,此方法的行为略有不同。 如果重新分析点是目录(例如装入点),则会卸载该目录并删除装入点。 此方法不会通过重新分析点递归。 如果重新分析点是指向文件的符号链接,则会删除重新分析点,而不是符号链接的目标。
在某些情况下,如果在文件资源管理器中打开了指定的目录,则 Delete 该方法可能无法删除它。