C# 删除文件夹下的所有文件
public bool DeleteFiles(string path) { if (Directory.Exists(path)==false) { MessageBox.Show("Path is not Existed!"); return false; } DirectoryInfo dir = new DirectoryInfo(path); FileInfo[] files = dir.GetFiles(); try { foreach (var item in files) { File.Delete(item.FullName); } if (dir.GetDirectories().Length != 0) { foreach (var item in dir.GetDirectories()) { if (!item.ToString().Contains("$") && (!item.ToString().Contains("Boot"))) { // Console.WriteLine(item); DeleteFiles(dir.ToString() + "\\" + item.ToString()); } } } Directory.Delete(path); return true; } catch (Exception) { MessageBox.Show("Delete Failed!"); return false; } }