在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以刪除文件夾及其所有內容。如果文件夾不存在,我們輸出一條消息表示文件夾不存在。