using System;
using System.IO;
class Program
{
static void Main()
{
// 定义要删除的文件夹路径
string folderPath = @"C:\path\to\your\folder";
try
{
// 检查文件夹是否存在
if (Directory.Exists(folderPath))
{
// 删除文件夹及其所有内容
Directory.Delete(folderPath, true);
Console.WriteLine("文件夹已成功删除。");
}
else
{
Console.WriteLine("指定的文件夹不存在。");
}
}
catch (Exception ex)
{
// 捕获并输出任何异常信息
Console.WriteLine("删除文件夹时发生错误: " + ex.Message);
}
}
}
string folderPath
来定义要删除的文件夹路径。Directory.Exists(folderPath)
方法来检查指定路径的文件夹是否存在。Directory.Delete(folderPath, true)
方法删除该文件夹及其所有内容。第二个参数 true
表示递归删除文件夹中的所有子文件夹和文件。try-catch
块捕获并处理可能发生的异常,例如权限不足或文件被占用等。上一篇:c# 写文本文件
下一篇:c#字符串数组
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站