Reputation: 34034
I have very weird behavior. I have,
Directory.Delete(tempFolder, true);
if (Directory.Exists(tempFolder))
{
}
Sometimes Directory.Exists return true. Why? May be the explorer is open?
Upvotes: 21
Views: 4230
Reputation: 28540
Use DirectoryInfo
instead, and call Refresh()
on that.
var dir = new DirectoryInfo(tempFolder);
dir.Delete();
dir.Refresh();
Because we are performing many operations on the directory, it is more performant to use DirectoryInfo
rather that Directory
. This probably explains why there is no Refresh()
on the static class, it is meant for one off operations and so would never need to be refreshed.
If might be worth adding a Thread.Sleep(0)
after the refresh to relinquish the thread and put it to the back of the pool. Haven't tested that though, it's just a musing.
Upvotes: 8
Reputation: 51499
Directory.Delete
calls the Windows API function RemoveDirectory
. The observed behavior is documented:
The
RemoveDirectory
function marks a directory for deletion on close. Therefore, the directory is not removed until the last handle to the directory is closed.
The .NET documentation is unfortunately missing this information. Whether the static Directory.Delete
method opens a handle to the directory is not documented. Likewise, if it does, it is not documented when the handle is closed.
Without any of this information, the best you can do is to poll for completion:
Directory.Delete(tempFolder, true);
while (Directory.Exists(tempFolder)) Thread.Sleep(0);
// At this point the directory has been removed from the filesystem
Even though polling should generally be avoided in preference of events, installing a filesystem watcher would be a bit over the top for this. Still, keep in mind, that this operation does not come for free, particularly when dealing with a network drive.
This information, however, does not help in finding a better solution than the one described above (polling for completion).
GC.Collect()
does not run finalizers. This, too, appears to work by buying extra time.
Upvotes: 30