7

I have a simple console application that I'm trying to delete a folder:

Directory.Delete(folder,true);

I got the exception of the annoying Thumbs.db

The process cannot access the file 'Thumbs.db' because it is being used by another process.

I can't change the registry to avoid Thumbnail to process in folder

What are my options here to be able to delete the folder with everything in it?

thanks

RollRoll
  • 8,133
  • 20
  • 76
  • 135
  • 1
    `thumbs.db` is used/owned by the Windows shell. Make sure the folder isn't visible in any window (that there are no open views on the folder), and you should be able to delete the file. – 500 - Internal Server Error Aug 02 '14 at 18:45
  • 1
    You may want to check [this link](http://www.hardanswers.net/stop-thumbs-db-from-locking-folder-deletion-renames) which goes straight to the point without needing to hack the registry **manually**. – ereOn Dec 18 '14 at 01:10
  • I normally work around this problem manually by renaming thumbs.db before I delete it. I have never tried doing that programmatically, but it's worth a try. – Bent Tranberg Jan 09 '22 at 14:19

3 Answers3

9

You can find out which process is blocking it with Unlocker. If you can't kill that process you can mark this file or folder to be deleted right after the next boot with MoveFileEx.

[DllImport("kernel32.dll")]
public static extern bool MoveFileEx(string lpExistingFileName, string lpNewFileName, int dwFlags);

public const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x4;

//Usage:
MoveFileEx(fileName, null, MOVEFILE_DELAY_UNTIL_REBOOT);


If you want to disable the creation of "Thumbs.db"-Files completely you can switch it off in the registry. Open the registry editor, navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer and set the value of NoThumbnailCache to 1. If this entry doesnt exist you simply can create it (DWORD 32).

For Win7 Professional / Ultimate the path to the entry is HKEY_CURRENT_USER\Software\Microsoft\ Windows\CurrentVersion\Explorer\Advanced and it's name is DisableThumbnailCache.

latonz
  • 1,601
  • 10
  • 21
0

To delete Thumbs.db files that were residing on a network drive for Windows 10, I had to go into 'Local Group Policy Editor' under 'User Configuration/Administrative Templates/ Windows Components/File Explorer' and Enable the 'Turn off the caching of thumbnails in hidden thumbs.db files' setting. Then go into Task Manager and restart Windows Explorer.

w. Patrick Gale
  • 1,643
  • 13
  • 22
-1

Deleting a file named thumbs.db is possible, by first renaming the file (e.g. into x.db). After renaming the file, deleting is no problem.

  • 1
    When the file is open in (and thus blocked by) Windows Explorer, it cannot be renamed. – Traveler Aug 21 '22 at 11:33
  • ```test> Rename-Item Thumbs.db bye.db Rename-Item : The process cannot access the file because it is being used by another process.``` – Nate May 12 '23 at 03:49