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}");
}
}
}
using System; 和 using System.IO; 用于引入必要的命名空间。string folderPath = @"C:\path\to\your\folder"; 设置要删除的文件夹路径。Directory.Exists(folderPath) 方法检查文件夹是否存在。Directory.Delete(folderPath, true); 方法删除文件夹及其所有内容(包括子文件夹和文件)。第二个参数 true 表示递归删除。try-catch 块捕获并处理可能的异常,例如权限问题或路径无效等。上一篇:c# 集合
下一篇:c# const
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站