5

In my example I am trying to delete the folders under a particular folder. My folder structure like this... C:\Export\MyDir1\MyDir2\MyDir3\MyDir4\MyDir5 This structure will come on the fly. Next time when I run my application it should check for C:\Export\MyDir1 directory and delete if exists. I written like this

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

I checked with the issue posted in this sit Issue I tried with this one but can't get solution. As per the suggested answer for this question, when i try to iterate through the directories then it is going to infinite loop. Where I done the mistake? Could any one help me?

4

2 回答 2

16

执行递归删除:Directory.Delete(exportTargetPath, true);

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

path指定的目录是只读的,或者recursive为false且path不是空目录。

于 2012-05-30T12:13:23.313 回答
5

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

于 2012-05-30T12:13:20.100 回答