我希望能够将文件夹的路径传递给应用程序,让程序运行该文件夹的整个内容,包括嵌套的文件夹和文件,删除它遇到的具有特定名称的任何文件夹.
我一直在寻找可能的方法,但我似乎找不到任何好的文档.
非常感谢帮助.
亲切的问候,
尝试这样的操作,删除在初始目录中找到的与您指定的名称匹配的任何目录:
public void RecursiveDelete(string path, string name)
{
foreach (string directory in Directory.GetDirectories(path))
{
if (directory.EndsWith("\\" + name))
{
Directory.Delete(directory, true);
}
else
{
RecursiveDelete(directory, name);
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后打电话 RecursiveDelete("initial path", "name of directory to delete");