c# - C#中的目录不为空错误

标签 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;
        }

我检查了本站发布的问题 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/

相关文章:

c# - 区域共享布局_ViewStart.cthml

c# - 如何在 PdfSharp 中添加指向文件的链接?

c# - 在 WPF/MVVM 上导航/加载不同的 View

c# - WebAPI 文件上传 - 无需将文件写入磁盘

C# dotnet core 2 将数据从中间件/过滤器传递到 Controller 方法

c# - C# MVC Ajax 请求的问题

c# - 一个类轮,如果呼吁采取行动

c# - 如何使用 FluentAssertions 测试嵌套集合

c# - 如何处理由 C++ 加载的 C# DLL 中的异常

c# - 将单个值附加或前置到 IEnumerable<T> 的简单方法是什么?