c#中的目录不是空错误

Sea*_*her 2 c#

在我的示例中,我试图删除特定文件夹下的文件夹.我的文件夹结构如下...... C:\Export\MyDir1\MyDir2\MyDir3\MyDir4\MyDir5这种结构将随时出现.下次运行我的应用程序时,应检查C:\Export\MyDir1目录并删除(如果存在).我是这样写的

private static string getExportPath(string exportTargetPath, string parentIssue)
        {
            string exportPath = Path.Combine(exportTargetPath, parentIssue);
            if (Directory.Exists(exportPath))
            {
                string[] files = Directory.GetFiles(exportPath);
                string[] dirs = Directory.GetDirectories(exportTargetPath);

                File.SetAttributes(exportTargetPath, FileAttributes.Normal);
                Directory.Delete(exportTargetPath,false);
            }

            return exportPath;
        }
Run Code Online (Sandbox Code Playgroud)

我检查了这个问题中发布的问题 我试过这个但是无法得到解决方案.根据这个问题的建议答案,当我尝试遍历目录时,它将进入无限循环.我在哪里做错了?任何人都可以帮助我吗?

Fis*_*rdo 6

做一个递归删除: Directory.Delete(exportTargetPath, true);

MSDN特别声明,如果出现以下情况,您将收到IOException:

path指定的目录是只读的,或者递归为false,path不是空目录.


MrS*_*ess 5

Directory.Delete的第二个参数被命名为“递归”是有原因的。尝试将其设置为 true。