Denis
Denis

Reputation: 12087

DirectoryInfo doesn't create directory

This has to be something silly but I just don't see it. So I have this code:

var dir = new DirectoryInfo("somedir");
if (dir.Exists) {
   dir.Delete(true);
}
dir.Create();

If the directory DOESN'T EXIST the directory is created just fine. If the directory EXISTS then no directory is created. Why?

Upvotes: 3

Views: 834

Answers (1)

NikolaiDante
NikolaiDante

Reputation: 18649

Try this:

var dir = new DirectoryInfo("somedir");
if (dir.Exists)
{
   dir.Delete(true);
   dir.Refresh();
}

dir.Create();

You need to refresh after the delete to update the state info.

Upvotes: 11

Related Questions