温馨提示×

c# removeall能删除文件夹吗

c#
小樊
84
2024-11-30 03:25:13
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在C#中,RemoveAll()方法通常用于删除文件,而不是文件夹。要删除文件夹及其内容,您需要使用Directory.Delete()方法。以下是一个示例:

using System;
using System.IO;

class Program
{
    static void Main()
    {
        string folderPath = @"C:\example_folder";

        if (Directory.Exists(folderPath))
        {
            Directory.Delete(folderPath, true); // 设置recursive参数为true以删除文件夹及其所有内容
            Console.WriteLine("Folder deleted successfully.");
        }
        else
        {
            Console.WriteLine("Folder does not exist.");
        }
    }
}

在这个示例中,我们首先检查文件夹是否存在。如果存在,我们使用Directory.Delete()方法删除它,并将recursive参数设置为true以删除文件夹及其所有内容。如果文件夹不存在,我们输出一条消息表示文件夹不存在。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:c# removeall如何删除非空文件夹

0