I have a button in my ASP.NET's web application, and when it's pushed, I simply delete the contents of a folder, and copy-paste some new fresh contents from another folder to it. Both folders are children of the root directory of this application, and everyone group has full access to this folder (just for testing purposes). To delete files and folders of the target folder, I use Directory.Delete
method and I return the attributes of each file to normal before deleting it which means that no file is read-only or protected when deleted. This works smoothly. But when someone presses that button sequentially (in less than 15 seconds or so), the second time it throws an exception and shows "The directory is not empty). What should I do? I don't know what the problem is. I think it should be something related to IO of Operating System (in my case, Windows).

- 5,753
- 72
- 57
- 129

- 35,341
- 41
- 136
- 188
3 Answers
Use the overloaded method, Directory.Delete(directoryToDelete, true);
Take a look at this method.
It says
Deletes the specified directory and, if indicated, any subdirectories and files in the directory.
The Directory.Delete
method you are using requires the directory to be empty.
Update
This question is asked here before.

- 1
- 1

- 19,542
- 20
- 83
- 128
-
Of course, sorry that I haven't mentioned that. I'm using `Directory.Delete(path, true)` overload. But the problems exists. – Saeed Neamati Jun 27 '11 at 08:43
-
It's ridiculous. This method works after 15 seconds pause. It seems that Microsoft does something under the hood. Something like a `Thread.sleep` – Saeed Neamati Jun 27 '11 at 08:46
-
Are you 100% sure that at that time, the directory is indeed completely empty? No hidden files ? – Ranhiru Jude Cooray Jun 27 '11 at 09:07
-
Well, if you read the help of the other overload (which takes a second Boolean argument) [here](http://msdn.microsoft.com/en-us/library/fxeahc5f.aspx), it says that there is no need for the directory to be empty, if you set the recursive argument to true. – Saeed Neamati Jun 27 '11 at 09:16
-
That is what I understood too and how I expect the function to work. However, the problem you are having has occurred to other developers as well. Please see the question i posted. – Ranhiru Jude Cooray Jun 27 '11 at 09:18
I think Directory.Delete(String, Boolean), returns before it's completed the deletion.
I've used the code below to refresh directories and the Sleep(100) is definitely being hit on occasions, but rarely on the first invocation (which corresponds to your description "specially (sic) in repetitive operation").
String destination = Path.Combine(targetFolder, Name);
if (Directory.Exists(destination))
{
Directory.Delete(destination, true);
Int32 i = 0;
while(Directory.Exists(destination))
{
System.Threading.Thread.Sleep(100);
if (i++ > 50) { throw new Exception("Failed to remove " + destination); }
}
}
Directory.CreateDirectory(destination);

- 475
- 1
- 3
- 14
I found the problem. I was trying to delete a read-only folder. Unfortunately, seems that .NET is not smart enough to show a relevant message.
However, always before copying, moving, or deleting any file or folder, set its attribute to normal:
File.SetAttributes(filePath, FileAttributes.Normal);

- 35,341
- 41
- 136
- 188