Directory.Delete(path, true) throws IOException "The directory is not empty"

I have code that creates a temp directory, does stuff, and then deletes the directory when it is done. The problem is that even though I specify true for the recursive parameter, it still throws an IOException saying "The directory is not empty". Here's what I have for code:

DirectoryInfo info = Directory.CreateDirectory(Path.Combine(tempdir, "temp"));
try{
    PopulateDir(info);
    foreach (FileInfo file in info.EnumerateFiles("*.*", SearchOption.AllDirectories)){
        DoStuff(file);
    }
}
finally{
    info.Delete(true);// note: this is apparently functionally identical to Directory.Delete(info.FullName, true)
}

Upvotes: 0

Views: 2168

Answers (1)

It appears that info.EnumerateFiles was the issue. I got that idea from this answer. I switched that to info.GetFiles and I was then able to delete the directory after.

Upvotes: 1

Related Questions