阅读量:4
在C#中,你可以使用System.IO命名空间中的Directory类来清空目录。以下是一个示例代码,展示了如何清空一个目录及其所有子目录和文件:
using System;
using System.IO;
class Program
{
static void Main()
{
string directoryPath = @"C:\path\to\your\directory";
try
{
// 清空目录
Directory.Delete(directoryPath, true);
Console.WriteLine("目录已成功清空。");
}
catch (Exception ex)
{
Console.WriteLine("清空目录时发生错误: " + ex.Message);
}
}
}
在这个示例中,Directory.Delete方法用于删除指定的目录及其所有子目录和文件。第二个参数true表示递归删除目录及其内容。如果目录为空,Directory.Delete方法将直接删除目录。如果在删除过程中发生任何错误,将抛出异常,你可以在catch块中处理这些异常。
以上就是关于“c# directory如何清空目录”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm