using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; namespace DeleteDirectoryTest { class Program { static void Main(string[] args) { string path = @"C:\Users\pengshiyu\Desktop\test"; if (Directory.Exists(path)) { try { Console.WriteLine("请输入删除方式:\n1、当文件夹为空时删除,\n2、删除所有子目录"); string choice = Console.ReadLine(); if (choice == "1") { Directory.Delete(path, false);//如果文件夹包涵子目录,则抛出异常 Console.WriteLine("删除空文件成功"); } else if (choice == "2") { Directory.Delete(path,true); Console.WriteLine("删除所有文件成功"); } else { Console.WriteLine("用户输入有误!"); } } catch (Exception ex) { Console.WriteLine(ex.Message); } } else { Console.WriteLine("文件不存在!" + path); } Console.ReadKey(); } } }