问题
Sometimes I can't delete a folder, because it contains thumbs.db. I have a lot of folders (which contain images too) and I will need to move them in a different place and delete the original folder. But I am getting this error: the folder can't be deleted because "thumbs.db" is being used by another process. Moving and deleting is made in c#, and right now it skips deleting the folders that have thumbs.db, which will result in a lot of empty folders. Is there a way to delete the folder even if it has that file?
回答1:
am submitting my comment as an answer because it is much more readable this way:
You can switch off a creation of these files: in Registry Editor go to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer and set NoThumbnailCache entry's value to 1.
If you don't have this entry , just create it (DWORD 32).
For Win 7 Ultimate/Professional version: HKEY_CURRENT_USER\Software\Microsoft\ Windows\CurrentVersion\Explorer\Advanced
DisableThumbnailCache (DWORD also)
To delete all already existing files create a batch file with the following content:
cd c:\
del /s /q thumbs.db
and run it as administrator
回答2:
Find out who is locking
First thing you should do is finding what which process is holding a lock. You can use Unlocker to find that out.
If you can't kill, control or quit that process
You can mark files for deletion. They will get deleted at the next startup. Use PInvoke and call MoveFileEx
passing null in as the destination.
You can use this for folders and files. Marking the folder for deletion should be enough.
This link has some sample code:
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);
public const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x4;
MoveFileEx(filename, null, MOVEFILE_DELAY_UNTIL_REBOOT);
回答3:
In my case simplest solution helped, however I am aware it is not going to help every time.
- Close File Explorer (this should unlock file)
- Open again and try to delete folder.
来源:https://stackoverflow.com/questions/9767629/delete-folder-that-contains-thumbs-db