簡體   English   中英

c#中的目錄不是空錯誤

[英]Directory is not empty error in 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;
        }

我檢查了這個問題中發布的問題我試過這個但是無法得到解決方案。 根據這個問題的建議答案,當我嘗試遍歷目錄時,它將進入無限循環。 我在哪里做錯了? 任何人都可以幫助我嗎?

執行遞歸刪除: Directory.Delete(exportTargetPath, true);

MSDN特別聲明,如果出現以下情況,您將收到IOException:

path指定的目錄是只讀的, 或者遞歸為false,path不是空目錄。

Directory.Delete的第二個參數由於某種原因被命名為“遞歸”。 嘗試將其設置為true。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM