In my app, I have built a system where users can create picture galleries. Photos held in folders in the format of category_name/gallery_name/{pictures} on disk. Each uploaded photo is stored under relevant directory structure as given above.
When trying to delete a category though, as well as deleting from the database, I want to delete relevant folders from the system too. When I first received the error message "Directory is not empty" I searched and found this solution:
public static void DeleteDirectory(string target_dir) { string[] files = Directory.GetFiles(target_dir); string[] dirs = Directory.GetDirectories(target_dir); foreach (string file in files) { File.SetAttributes(file, FileAttributes.Normal); File.Delete(file); } foreach (string dir in dirs) { DeleteDirectory(dir); } Directory.Delete(target_dir, false); }
With this solution, photos in the "gallery_name" folder gets deleted just fine, then the gallery_name folder itself gets deleted fine.. so we are now left with an empty category_name folder. Then the last bit of code in the above subroutine (Directory.Delete(target_dir, false);
) gets called to delete the category_name folder. The error raises again..
Does anyone knows a solution to this?
Directory.Delete(target_dir, true);
did not work, that is why I tried an alternative.The exception message I get is:
System.IO.IOException was unhandled by user code HResult=-2147024751 Message=The directory is not empty. Source=mscorlib StackTrace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound) at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive, Boolean checkHost) at System.IO.Directory.Delete(String path) at MyApp.PhotoGallery.Categories.deleteCategory(Int32 cID, String categoryname) in d:\Documents\My Dropbox\web projects\MyAppFolder\App_Code\BLL\PhotoGallery.vb:line 291 at _admhades_PhotoGallery.deleteCategory(Int32 categoryID, String categoryname) in d:\Documents\My Dropbox\web projects\HavadisPre\_admhades\PhotoGallery.aspx.vb:line 71
One of the common issues I've encountered is one that involves folders not deleting properly. When this issue occurs, an error message appears that says “Cannot Delete foldername: The directory is not empty“. This problem can happen in Windows 10, 8, and 7. The problem can usually be solved with a Chkdsk scan.
If the directory is not empty or you do not have permission to delete it, you will see an error message. To remove a directory that is not empty, use the rm command with the -r option for recursive deletion.
What is error 0x80070091 The directory is not empty? It mainly occurs when you try to delete a folder from an external hard disk or SD card or pen drive, but it can also happen when deleting a file from the system drive.
You may just use Directory.Delete(target_dir, true);
to remove directory and all files recursively. You don't have to write custom function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With