Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rebuild Windows Icon Cache without closing explorer.exe or restarting Windows?

Tags:

winapi

Task

I am trying to customise a folder icon from within my Application but due to the way Windows caches icons and thumbnails I am struggling to get the changed folder to update it's new icon.

In Windows there are two ways that I know of to customise a folders icon, the simplest way is to right click the folder from Windows Explorer, Properties and then select the Customize tab and change its icon, this method is no good though as it requires manually changing the folder properties.

The second way which I am doing from my program is to create a desktop.ini file and place it inside the folder that requires the icon changing, documentation on desktop.ini can be found here: http://hwiegman.home.xs4all.nl/desktopini.html and an article here: http://helpdeskgeek.com/how-to/customize-folder-icons-desktop-ini/

As shown below though, the folder icon in Windows is not updated although you can see from the folder properties that the icon has changed, just Windows is not showing the updated folder icon:

enter image description here

I am fully aware of Windows caching icons and thumbnails, I have searched and read several pages on ways to supposedly rebuild the icon cache and thus get Windows to display the newly changed icon and not the cached one, however I am unable to get this to work from my Application without restarting Windows or the Explorer.exe process, this needs to be done without interrupting Windows though so closing Explorer.exe or requiring a restart is absolutely not an option.

Interestingly though, when changing a folder icon from the first way I described earlier, Windows somehow manages to flush and rebuild the icon cache as the changed folder is promptly updated to show the new icon, however I cannot see what tricks Windows does to achieve this.

Attempts

A few things I tried unsucessfully, (in no particular order):

  • Executing the following: ie4uinit.exe -ClearIconCache
  • Running this code in Lazarus: SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
  • Deleting the IconCache.db file from AppData\Local
  • Toggling the "Always show icons, never thumbnails" Explorer option from the registry, located here: HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Advanced which is stored in the IconsOnly value
  • Changing the icon size from the registry and back, located here: HKEY_CURRENT_USER\Control Panel\Desktop\WindowMetrics which is stored in the Shell Icon Size value
  • This link provides an apparent solution in Delphi which I was unable to convert in Lazarus: http://www.aha-soft.com/faq/q18.htm
  • I am aware of cached db files here: AppData\Local\Microsoft\Windows\Explorer although I am not sure if they are of any help, attempting to delete some of them does not seem possible as they may be locked etc.
  • Renaming the folder and then rename back to original name.
  • Right click an empty space in Windows Explorer and selecting Refresh does not seem to update the folder icon either.

Have also tried performing some of the above in combination with no luck.

Question

When changing a folder icon directly from Windows Explorer, using the properties context menu and customize tab, when applying and closing this dialog the folder icon is changed almost immediately, there is no closing explorer.exe or restarting Windows, the change happens very much right away.

How can I achieve this from my Application programmatically the same way Windows Explorer does? Is there a way to find out how Windows Explorer does this?

My Operating System is Windows 10 which could explain why some of the older methods don't work for me.

like image 667
Craig Avatar asked Jan 14 '16 20:01

Craig


People also ask

How to perform icon cache rebuild on Windows 11/10 with command prompt?

In the following content, you can learn how to perform icon cache rebuild on Windows 11/10 with Command Prompt. Step 1: Press Win + S to open the Windows Search utility. Then search for Command Prompt and run it as administrator. Step 2: In Command Prompt, input the commands below one by one and press Enter after each:

How to clear icons cache in Windows 10?

Press Windows key+X and click on “Run” from the menu that pops up near the taskbar. Enter the following command on a Windows 10 PC to instantly clear icon cache. If you have any other version of Windows installed, type the following command to clear icon cache.

How do I open the icon cache database in Windows 10?

Open Start and do a search for Command Prompt. Right-click the result and select Run as administrator. Type the following command to navigate to the icon cache database location and press Enter: Type the following command to verify the icon cache database files are at the location and press Enter:

What is a cache file in Windows Explorer?

These cache files contain small thumbnails for different files on your computer and Windows Explorer loads the thumbnails from these files as opposed to generating thumbnails for each image again and again when you open a folder in it.


1 Answers

I don't think this is anything to do with icon caching; you just need to inform Explorer that the folder's icon has changed. You can do this using the SHChangeNotify function.

LPCWSTR pszFolder = L"c:\\path\\to\\folder";
SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH, pszFolder, NULL);
like image 181
Jonathan Potter Avatar answered Jan 02 '23 20:01

Jonathan Potter