递归删除目录
最近我在用svn,用过svn的朋友都知道,它会在每个目录下面添加 .svn隐藏文件。由于我的程序目录结构比较多,刚开始手动的删除.svn目录,删了一段时间,感觉比较慢,想为什么不写个程序,来帮助我来删除.svn目录呢。后来经过看一些资料,把程序写了出来。
其实这个程序比较的简单,也对当前目录进行判断,如果含有指定的目录,则将该目录删除,没有的话,如果含有其他目录,然后进行递归操作。将含有指定的目录删除即可。代码如下:
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace DirOper
{
/**//// <summary>
/// 删除子目录
/// </summary>
public class DelSubDir
{
private string _sourcePath; //操作的路径
private string _delDirName; //删除的目录名称
/**//// <summary>
/// 删除目录所在的原始路径
/// </summary>
public string SourcePath
{
get { return this._sourcePath; }
set { this._sourcePath = value; }
}
/**//// <summary>
/// 进行删除的目录名称
/// </summary>
public string DelDirName
{
get { return this._delDirName;}
set { this._delDirName = value; }
}
/**//// <summary>
/// 开始删除目录
/// </summary>
public void DeleteDir()
{
DirectoryInfo dirinfo = new DirectoryInfo(this.SourcePath);
this.ListDirectory(dirinfo);
}
/**//// <summary>
/// 列出目录信息
/// </summary>
/// <param name="info">FileSystemInfo类</param>
private void ListDirectory(FileSystemInfo info)
{
if (!info.Exists)
{
Console.WriteLine("目标路径不正确,请重新输入!");
return;
}
DirectoryInfo dir = info as DirectoryInfo;
if (dir == null)
{
Console.WriteLine("目标路径不是一个目录!");
return;
}
try
{
this.ForeachDir(dir);
Console.WriteLine("对目录 " + this.SourcePath + ",删除目录名称为:" + this.DelDirName + " ,操作完成!");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);//抛出异常
}
}
/**//// <summary>
/// 递归遍历目录
/// </summary>
/// <param name="info">目录信息</param>
private void ForeachDir(DirectoryInfo info)
{
foreach (DirectoryInfo mydir in info.GetDirectories())
{
if (mydir.Name == this.DelDirName)
{
mydir.Delete(true);
Console.WriteLine("删除了文件:" + mydir.FullName);
}
else
ForeachDir(mydir);
}
}
}
public class client
{
public static void Main()
{
//实例化类
DelSubDir delDir = new DelSubDir();
Console.Write("请输入要删除目录的目标路径");
delDir.SourcePath = Console.ReadLine();
Console.Write("请输入要删除在目标路径下要删除的目录名称");
delDir.DelDirName = Console.ReadLine();
//执行删除操作
delDir.DeleteDir();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace DirOper
{
/**//// <summary>
/// 删除子目录
/// </summary>
public class DelSubDir
{
private string _sourcePath; //操作的路径
private string _delDirName; //删除的目录名称
/**//// <summary>
/// 删除目录所在的原始路径
/// </summary>
public string SourcePath
{
get { return this._sourcePath; }
set { this._sourcePath = value; }
}
/**//// <summary>
/// 进行删除的目录名称
/// </summary>
public string DelDirName
{
get { return this._delDirName;}
set { this._delDirName = value; }
}
/**//// <summary>
/// 开始删除目录
/// </summary>
public void DeleteDir()
{
DirectoryInfo dirinfo = new DirectoryInfo(this.SourcePath);
this.ListDirectory(dirinfo);
}
/**//// <summary>
/// 列出目录信息
/// </summary>
/// <param name="info">FileSystemInfo类</param>
private void ListDirectory(FileSystemInfo info)
{
if (!info.Exists)
{
Console.WriteLine("目标路径不正确,请重新输入!");
return;
}
DirectoryInfo dir = info as DirectoryInfo;
if (dir == null)
{
Console.WriteLine("目标路径不是一个目录!");
return;
}
try
{
this.ForeachDir(dir);
Console.WriteLine("对目录 " + this.SourcePath + ",删除目录名称为:" + this.DelDirName + " ,操作完成!");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);//抛出异常
}
}
/**//// <summary>
/// 递归遍历目录
/// </summary>
/// <param name="info">目录信息</param>
private void ForeachDir(DirectoryInfo info)
{
foreach (DirectoryInfo mydir in info.GetDirectories())
{
if (mydir.Name == this.DelDirName)
{
mydir.Delete(true);
Console.WriteLine("删除了文件:" + mydir.FullName);
}
else
ForeachDir(mydir);
}
}
}
public class client
{
public static void Main()
{
//实例化类
DelSubDir delDir = new DelSubDir();
Console.Write("请输入要删除目录的目标路径");
delDir.SourcePath = Console.ReadLine();
Console.Write("请输入要删除在目标路径下要删除的目录名称");
delDir.DelDirName = Console.ReadLine();
//执行删除操作
delDir.DeleteDir();
}
}
}