“Directory is not empty” error when trying to programmatically delete a folder

后端 未结 5 670
忘了有多久
忘了有多久 2020-12-16 09:19

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 upload

5条回答
  •  囚心锁ツ
    2020-12-16 10:00

    This isn't as complete as I'd like, but these are things that have helped me in the past when I faced similar issues.

    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

提交回复
热议问题