C#删除指定目录空文件夹(源码)
卸载软件的时候有些软件只删除程序,并不删除文件夹 以前不会编程,网上找教程大部分都是bat文件的教程 (放到想要删除的目录下运行,但这只能删除这个目录下的空目录,不能删除这个目录下空目录的子目录) 为了方便自己就动手写了一个
Main.cs
-
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
using System.Data;
-
using System.Drawing;
-
using System.IO;
-
using System.Linq;
-
using System.Text;
-
using System.Threading.Tasks;
-
using System.Windows.Forms;
-
-
namespace Delete
-
{
-
public partial class Main : Form
-
{
-
public Main()
-
{
-
InitializeComponent();
-
}
-
-
// 浏览
-
private void btnLL_Click(object sender, EventArgs e)
-
{
-
FolderBrowserDialog f = new FolderBrowserDialog();
-
if (f.ShowDialog() == DialogResult.OK)
-
{
-
String DirPath = f.SelectedPath;
-
this.txtPath.Text = DirPath;//G:\\新建文件夹
-
}
-
}
-
-
// 开始
-
private void btnDelete_Click(object sender, EventArgs e)
-
{
-
// 获取路径
-
string str = txtPath.Text;
-
str.Replace(\"\\\\\", \"/\");
-
if (str.Equals(\"\"))
-
{
-
MessageBox.Show(\"路径不能为空!\", \"提示\");
-
}
-
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:\\\\\"))
-
{
-
MessageBox.Show(\"路径不能为盘符!\", \"提示\");
-
}
-
else
-
{
-
getPath(str);
-
}
-
}
-
-
-
static List<string> list = new List<string>();//定义list变量
-
public List<string> getPath(string path)
-
{
-
// 获取子目录
-
DirectoryInfo dir = new DirectoryInfo(path);
-
FileInfo[] fil = dir.GetFiles();
-
DirectoryInfo[] dii = dir.GetDirectories();
-
foreach (FileInfo f in fil)
-
{
-
list.Add(f.FullName);//添加文件的路径到列表
-
}
-
//获取子文件夹内的文件列表,递归遍历
-
foreach (DirectoryInfo d in dii)
-
{
-
getPath(d.FullName);
-
list.Add(d.FullName);//添加文件夹的路径到列表
-
}
-
-
// 删除空目录
-
/// 删除掉空文件夹
-
/// 所有没有子“文件系统”的都将被删除
-
DirectoryInfo[] subdirs = dir.GetDirectories(\"*.*\", SearchOption.AllDirectories);
-
foreach (DirectoryInfo subdir in subdirs)
-
{
-
FileSystemInfo[] subFiles = subdir.GetFileSystemInfos();
-
if (subFiles.Count() == 0)
-
{
-
subdir.Delete();
-
}
-
}
-
-
return list;
-
}
-
-
-
-
// 文本框
-
private void txtPath_Click(object sender, EventArgs e)
-
{
-
FolderBrowserDialog f = new FolderBrowserDialog();
-
if (f.ShowDialog() == DialogResult.OK)
-
{
-
String DirPath = f.SelectedPath;
-
this.txtPath.Text = DirPath;//G:\\新建文件夹
-
}
-
}
-
-
// 乐特 s\' Blog
-
private void lblLete_Click(object sender, EventArgs e)
-
{
-
System.Diagnostics.Process.Start(\"http://lete114.github.io/\");
-
}
-
}
-
}
Program.cs
-
using System;
-
using System.Collections.Generic;
-
using System.Linq;
-
using System.Threading.Tasks;
-
using System.Windows.Forms;
-
-
namespace Delete
-
{
-
static class Program
-
{
-
/// <summary>
-
/// 应用程序的主入口点。
-
/// </summary>
-
[STAThread]
-
static void Main()
-
{
-
Application.EnableVisualStyles();
-
Application.SetCompatibleTextRenderingDefault(false);
-
Application.Run(new Main());
-
}
-
}
-
}
仅供参考