Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete Thumbs.db (it is being used by another process)

Tags:

c#

.net

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

like image 447
RollRoll Avatar asked Aug 02 '14 18:08

RollRoll


Video Answer


1 Answers

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.

like image 127
latonz Avatar answered Oct 11 '22 23:10

latonz