The directory is not empty cannot delete error

Description

I got error The directory is not empty cannot delete while deleting some folders in my Windows 8 machine. but when check the folder, I don’t find any suspected files regarding error this error. I have deleted many times this kind of folders with lot files inside, but today only I got this error.

Solution – The directory is not empty cannot delete

After I have googled and analyzed, I noticed the reason for this error is due to Hidden files which are inside the folders.

try to delete folder with this command

rmdir /s C:UsersYourFolderfolder
- or -
rmdir /s/q C:UsersYourFolderfolder

Solution – The directory is not empty cannot delete in C#

If you got this error while deleting folder from C# code you need to use following code to set folder attributes into Normal.

File.SetAttributes(target_dir, FileAttributes.Normal);
Directory.Delete(target_dir, false);



Useful References:

– http://www.codingforums.com/showthread.php?t=217642

– http://forums.techguy.org/all-other-software/1019285-cannot-delete-folder.html

– http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true

Thanks,
Morgan
Software Developer

Advertisement

Leave a Comment