C#删除指定目录空文件夹

C#删除指定目录空文件夹(源码)

卸载软件的时候有些软件只删除程序,并不删除文件夹 以前不会编程,网上找教程大部分都是bat文件的教程 (放到想要删除的目录下运行,但这只能删除这个目录下的空目录,不能删除这个目录下空目录的子目录) 为了方便自己就动手写了一个

Main.cs

  1. using System;

  2. using System.Collections.Generic;

  3. using System.ComponentModel;

  4. using System.Data;

  5. using System.Drawing;

  6. using System.IO;

  7. using System.Linq;

  8. using System.Text;

  9. using System.Threading.Tasks;

  10. using System.Windows.Forms;

  11.  

  12. namespace Delete

  13. {

  14. public partial class Main : Form

  15. {

  16. public Main()

  17. {

  18. InitializeComponent();

  19. }

  20.  

  21. // 浏览

  22. private void btnLL_Click(object sender, EventArgs e)

  23. {

  24. FolderBrowserDialog f = new FolderBrowserDialog();

  25. if (f.ShowDialog() == DialogResult.OK)

  26. {

  27. String DirPath = f.SelectedPath;

  28. this.txtPath.Text = DirPath;//G:\新建文件夹

  29. }

  30. }

  31.  

  32. // 开始

  33. private void btnDelete_Click(object sender, EventArgs e)

  34. {

  35. // 获取路径

  36. string str = txtPath.Text;

  37. str.Replace("\\", "/");

  38. if (str.Equals(""))

  39. {

  40. MessageBox.Show("路径不能为空!", "提示");

  41. }

  42. else if (str.Equals("Q:\\") || str.Equals("W:\\") || str.Equals("E:\\") || str.Equals("R:\\") || str.Equals("T:\\") || str.Equals("Y:\\") || str.Equals("U:\\") || str.Equals("I:\\") || str.Equals("O:\\") || str.Equals("P:\\") || str.Equals("A:\\") || str.Equals("S:\\") || str.Equals("D:\\") || str.Equals("G:\\") || str.Equals("H:\\") || str.Equals("J:\\") || str.Equals("K:\\") || str.Equals("L:\\") || str.Equals("Z:\\") || str.Equals("X:\\") || str.Equals("C:\\") || str.Equals("C:\\") || str.Equals("V:\\") || str.Equals("B:\\") || str.Equals("N:\\") || str.Equals("M:\\"))

  43. {

  44. MessageBox.Show("路径不能为盘符!", "提示");

  45. }

  46. else

  47. {

  48. getPath(str);

  49. }

  50. }

  51.  

  52.  

  53. static List list = new List();//定义list变量

  54. public List getPath(string path)

  55. {

  56. // 获取子目录

  57. DirectoryInfo dir = new DirectoryInfo(path);

  58. FileInfo[] fil = dir.GetFiles();

  59. DirectoryInfo[] dii = dir.GetDirectories();

  60. foreach (FileInfo f in fil)

  61. {

  62. list.Add(f.FullName);//添加文件的路径到列表

  63. }

  64. //获取子文件夹内的文件列表,递归遍历

  65. foreach (DirectoryInfo d in dii)

  66. {

  67. getPath(d.FullName);

  68. list.Add(d.FullName);//添加文件夹的路径到列表

  69. }

  70.  

  71. // 删除空目录

  72. /// 删除掉空文件夹

  73. /// 所有没有子“文件系统”的都将被删除

  74. DirectoryInfo[] subdirs = dir.GetDirectories("*.*", SearchOption.AllDirectories);

  75. foreach (DirectoryInfo subdir in subdirs)

  76. {

  77. FileSystemInfo[] subFiles = subdir.GetFileSystemInfos();

  78. if (subFiles.Count() == 0)

  79. {

  80. subdir.Delete();

  81. }

  82. }

  83.  

  84. return list;

  85. }

  86.  

  87.  

  88.  

  89. // 文本框

  90. private void txtPath_Click(object sender, EventArgs e)

  91. {

  92. FolderBrowserDialog f = new FolderBrowserDialog();

  93. if (f.ShowDialog() == DialogResult.OK)

  94. {

  95. String DirPath = f.SelectedPath;

  96. this.txtPath.Text = DirPath;//G:\新建文件夹

  97. }

  98. }

  99.  

  100. // 乐特 s' Blog

  101. private void lblLete_Click(object sender, EventArgs e)

  102. {

  103. System.Diagnostics.Process.Start("http://lete114.github.io/");

  104. }

  105. }

  106. }

Program.cs

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Threading.Tasks;

  5. using System.Windows.Forms;

  6.  

  7. namespace Delete

  8. {

  9. static class Program

  10. {

  11. ///

  12. /// 应用程序的主入口点。

  13. ///

  14. [STAThread]

  15. static void Main()

  16. {

  17. Application.EnableVisualStyles();

  18. Application.SetCompatibleTextRenderingDefault(false);

  19. Application.Run(new Main());

  20. }

  21. }

  22. }

 仅供参考

你可能感兴趣的:(C#删除指定目录空文件夹)