0

I am trying to delete a directory and all its contents like this:

    private static void CleanOrCreateDirectory(string directory)
    {
        if (Directory.Exists(directory)) Directory.Delete(directory, true);
        Directory.CreateDirectory(directory);
    }

which should be the standard way, suggested in various answers to related questions on SO. However, it doesn't work. I get:

System.IO.IOException : The directory is not empty.

Well, of course it is not empty, that is the whole point! What am I doing wrong?

Grzenio
  • 35,875
  • 47
  • 158
  • 240

2 Answers2

0

There's a list of IOException causes when calling Directory.Delete here: http://msdn.microsoft.com/en-us/library/fxeahc5f.aspx

It lists all the distinct reasons why you get an IOException, one would assume the cause is listed there!

SpaceBison
  • 3,704
  • 1
  • 30
  • 44
0

It turns out that the implementation of Directory.Delete(name, true) just doesn't do what it says on the tin. For a workaround, see the accepted answer from Cannot delete directory with Directory.Delete(path, true).

Thanks @HaunsTM

Community
  • 1
  • 1
Grzenio
  • 35,875
  • 47
  • 158
  • 240