递归算法的实际应用

 1 static void DeleteFolder(string dir)
 2 {
 3     foreach (string d in Directory.GetFileSystemEntries(dir))
 4     {
 5         //判断路径是否存在
 6         if (File.Exists(d))
 7         {
 8             FileInfo fi = new FileInfo(d);
 9             //去除文件夹的只读属性
10             if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
11                 fi.Attributes = FileAttributes.Normal;
12             File.Delete(d);//直接删除其中的文件  
13         }
14         else
15         {
16             DirectoryInfo d1 = new DirectoryInfo(d);
17             if (d1.GetFiles().Length != 0)
18             {
19                 DeleteFolder(d1.FullName);////递归删除子文件夹
20             }
21             Directory.Delete(d);
22         }
23     }
24 }
原文地址:https://www.cnblogs.com/Johnfx-home/p/8624197.html