删除路径下的文件以及子路径。(范例“E:/www/”)

 1 /// <summary>
 2         /// 删除路径下的文件以及子路径。
 3         /// </summary>
 4         /// <param name="dir">目标路径</param>
 5         public static void DeleteFolder(string dir)
 6         {
 7             foreach (string d in Directory.GetFileSystemEntries(dir))
 8             {
 9                 if (File.Exists(d))
10                 {
11                     FileInfo fi = new FileInfo(d);
12                     if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
13                         fi.Attributes = FileAttributes.Normal;
14                     File.Delete(d);//直接删除其中的文件  
15                 }
16                 else
17                 {
18                     DirectoryInfo d1 = new DirectoryInfo(d);
19                     if (d1.GetFiles().Length != 0)
20                     {
21                         DeleteFolder(d1.FullName);////递归删除子文件夹
22                     }
23                     Directory.Delete(d);
24                 }
25             }
26         }
原文地址:https://www.cnblogs.com/luxishi/p/6646328.html