处理了以下问题
System.IO.Directory.Delete 目录不是空的。
对路径“License.rtf”的访问被拒绝 这个文件为只读属性
当这个文件夹的子文件夹处于打开时删除失败
using System.IO;
/// <summary> /// 取消目录下的所有文件夹及子文件的只读属性 /// </summary> /// <param name="dirPath"></param> private void DirectorySubFileCancelReadOnly(string dirPath) { string[] dirPathes = Directory.GetDirectories(dirPath, "*.*", SearchOption.AllDirectories); string[] filePathes = Directory.GetFiles(dirPath, "*.*", SearchOption.AllDirectories); foreach (var dp in dirPathes) { DirectoryInfo dir = new DirectoryInfo(dirPath); dir.Attributes = FileAttributes.Normal & FileAttributes.Directory; } foreach (var fp in filePathes) { File.SetAttributes(fp, System.IO.FileAttributes.Normal); } }
private void button1_Click(object sender, EventArgs e) { string s=@"D:AdministratorDocumentsMyProjectCSHARP_WinCtrlWindowsFormsApplication1inDebug我的文件夹\__history"; try { DirectorySubFileCancelReadOnly(s); } catch (System.Exception ex) { } finally { System.IO.Directory.Delete(s, true); } }