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);
}
}
本文介绍了一个用于递归删除文件系统中指定名称目录的方法。该方法首先检查并获取目标目录的名称,然后根据名称进行匹配删除。若当前路径的名称与指定名称相符,则设置文件属性为正常,并删除该目录及其内容;若不相符,则递归遍历子目录继续查找匹配项。
1238





