“Directory is not empty” error when trying to programmatically delete a folder-Collection of common programming errors

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?

EDIT: 1) Directory.Delete(target_dir, true); did not work, that is why I tried an alternative.. 2) I have full control over the parent folder and the category_name and gallery_name folders are also created programmatically without any problem.. 3) As I mentioned, the sub directories (gallery_name folders) and their contents (photos) are deleted with this code just fine.. It is the category_name folder which causes the error, even though after this code, it is just an empty folder… EDIT:

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

  1. You may just use Directory.Delete(target_dir, true); to remove directory and all files recursively. You don’t have to write custom function.

  2. I have the same issues. This is not an answer (not for my problem any way) but aS I’m still investigating the cause I thought I would leave this as more of a collection of thoughts and things I’ve tried which may help you (and others).

    The file is in use by something else. This means you’ve created a folder/file and have not yet released it. Use .Close() (where applicable).

    A lock related issue.

    You have used the Directory.Delete(rootFolder, true) (where true means delete recursively) when there are no folders within the root folder specified.

    It is open by another program. Now, I have NO idea where to begin on this other than installing Process Monitor which can help but that only really works in some situations.

    Things like Anti Virus, or even (on Windows) things like Defender have caused this issue in the past.

    When you call Directory.Delete and a file is open in such way, Directory.Delete succeeds in deleting all files but when Directory.Delete calls RemoveDirectory a “directory is not empty” exception is thrown because there is a file marked for deletion but not actually deleted.

    The obvious things includes make sure you have permission to delete the folder (not just you, but the account the program runs under).

    The file you are trying to delete is readonly. Change the file attributes of all files in the folder first from read-only.

    The path is shared by other Windows components so becareful where you are creating/deleting folders.

    Source
    Source