Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DirectoryInfo doesn't create directory

Tags:

c#

.net

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?

like image 925
Denis Avatar asked Feb 09 '26 17:02

Denis


1 Answers

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.

like image 60
NikolaiDante Avatar answered Feb 12 '26 15:02

NikolaiDante