public static void ClearDirectory(string dirPath) { string[] filePaths = Directory.GetFiles(dirPath); foreach (string file in filePaths) { File.Delete(file); } foreach (string childDirPath in Directory.GetDirectories(dirPath)) { FileHelper.DeleteDirectory(childDirPath); } }
/// <summary> /// 删除文件夹 /// </summary> public static void DeleteDirectory(string dirPath) { foreach (string filePath in Directory.GetFiles(dirPath)) { File.Delete(filePath); } foreach (string childDirPath in Directory.GetDirectories(dirPath)) { FileHelper.DeleteDirectory(childDirPath); } DirectoryInfo dir = new DirectoryInfo(dirPath); dir.Refresh(); if ((dir.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { dir.Attributes &= ~FileAttributes.ReadOnly; } dir.Delete(); }