Directory.Delete 方法 (System.IO)

您所在的位置:网站首页 newfolder文件夹 Directory.Delete 方法 (System.IO)

Directory.Delete 方法 (System.IO)

2023-10-07 13:10| 来源: 网络整理| 查看: 265

删除指定的目录,并删除该目录中的所有子目录和文件(如果表示)。

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

若要删除 path 中的目录、子目录和文件,则为 true;否则为 false。

例外 IOException

存在具有相同名称和 path 指定的位置的文件。

- 或 -

path 指定的目录为只读,或者 recursive 的值为 false,且 path 不是空目录。

- 或 -

该目录是应用程序的当前工作目录。

- 或 -

该目录包含一个只读文件。

- 或 -

另一个进程正在使用该目录。

UnauthorizedAccessException

调用方没有所要求的权限。

ArgumentException

.NET Framework 和 2.1 之前的 .NET Core 版本:path是一个零长度字符串,仅包含空格,或者包含一个或多个无效字符。 你可以使用 GetInvalidPathChars() 方法查询无效字符。

ArgumentNullException

path 为 null。

PathTooLongException

指定的路径和/或文件名超过了系统定义的最大长度。

DirectoryNotFoundException

不存在或找不到 path。

- 或 -

指定的路径无效(例如,它位于未映射的驱动器上)。

示例

以下示例演示如何在子目录中创建新目录、子目录和文件,然后以递归方式删除所有新项。

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 方法可能无法删除它。

另请参阅 文件和流 I/O 如何:从文件中读取文本 如何:将文本写入文件 适用于


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3