您的位置:首页 > 编程语言 > C#

C#批量删除指定文件夹下指定文件名的所有文件夹

2010-05-19 12:37 239 查看
private void DeleteDirByName(string rootPath, string name)
{
string dirName = rootPath;
if(rootPath.EndsWith("//")||rootPath.EndsWith("/"))
{
rootPath = rootPath.Substring(0,rootPath.Length-1);
}
int indexSplit = rootPath.LastIndexOf('//');
if(indexSplit<0)
{
indexSplit = rootPath.LastIndexOf('/');
}
if(indexSplit>0)
{
dirName = rootPath.Substring(indexSplit + 1);
}
if (dirName.ToLower() == name.ToLower())
{
this.SetFileAttributes(rootPath);
Directory.Delete(rootPath, true);
this.textBox3.Text += rootPath + Environment.NewLine;
}
else
{
string[] subDirs = Directory.GetDirectories(rootPath);
foreach (string subDir in subDirs)
{
this.DeleteDirByName(subDir, name);
}
}
}
private void SetFileAttributes(string path)
{
string[] files = Directory.GetFiles(path);
foreach (string file in files)
{
File.SetAttributes(file, FileAttributes.Normal);
}
string[] subDirs = Directory.GetDirectories(path);
foreach (string subDir in subDirs)
{
this.SetFileAttributes(subDir);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: