在我的示例中,我试图删除特定文件夹下的文件夹。我的文件夹结构是这样的... 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;
}
我检查了本站发布的问题 Issue我试过这个但无法得到解决方案。根据这个问题的建议答案,当我尝试遍历目录时,它将进入无限循环。我哪里做错了?谁能帮帮我?
最佳答案
执行递归删除:Directory.Delete(exportTargetPath, true);
MSDN具体说如果出现以下情况,您将获得 IOException:
The directory specified by path is read-only, or recursive is false and path is not an empty directory.
关于c# - C#中的目录不为空错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10815998/