3

I am now programming a small tool and need to refresh the folder icon instantly.
As we know, in Windows, we could modify folder icons by the following steps manually:

  1. right click the folder
  2. choose "customize" tag
  3. click "change icon"

I also know how to set the desktop.ini file to modify its icon. But it takes really a long time to refresh (about 30 seconds).

I wanna know if there is a common way to solve it instantly no matter using C/C++ or script. Or Windows never provides a way?

If possible, any code is welcome.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Neko Gong
  • 53
  • 4

1 Answers1

4

This small C program will do the job:

#include <windows.h>
#include <ShlObj.h>

const char folderpath[] = "C:\\Your-Folder";

int main() {
  SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH, folderPath, NULL);
}

folderpath is the full path to the folder whose icon is to be updated Following sequence of operations works fine here (Windows 10 64 bits):

Desktop.ini file

[.ShellClassInfo]
IconResource=C:\Windows\System32\SHELL32.dll,12
  1. Create a folder X
  2. Copy the Desktop.ini file above into the folder. The icon of the X folder will not change
  3. Run the small C program above
  4. The new icon is displayed on the X folder
  5. Remove the Deskop.ini file created at point 2. The icon of the X folder will not change
  6. Run the small C program above
  7. The original folder icon is displayed on the X folder

Check the SHChangeNotify function for more details.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Thank you for your detailed solution! I will have a try. Thanks again. – Neko Gong Aug 28 '17 at 15:46
  • 1
    It would be worth a try if `SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH, folderPath, NULL);` is sufficient. Should be more efficient than invalidating the whole icon cache when only one icon changed. Found at [OldNewThing](https://blogs.msdn.microsoft.com/oldnewthing/20150903-00/?p=91671). – zett42 Aug 28 '17 at 16:33
  • 1
    Yesterday I played around with `SHCNE_UPDATEITEM` but couldn't get consistent results. Sometimes it would update the folder icon, sometimes not. I also tried to add `SHCNF_FLUSH` and `SHCNE_UPDATEDIR` but the result was still unreliable. – zett42 Aug 29 '17 at 07:04
  • 2
    I did some further tests where I found that the supposed "catch all" `SHCNE_ASSOCCHANGED` is unreliable aswell. But [SHGetSetFolderCustomSettings()](https://stackoverflow.com/a/19440996/7571258) always updates the icon immediately (despite being a deprecated API since Win XP SP3)! – zett42 Aug 29 '17 at 16:57
  • I searched and found https://learn.microsoft.com/en-us/windows/desktop/shell/how-to-customize-folders-with-desktop-ini which links here https://learn.microsoft.com/en-us/windows/desktop/api/Shlwapi/nf-shlwapi-pathmakesystemfoldera and tested it: After giving my still default looking folder the desktop.ini file, I used "attrib +S" ON THE FOLDER. And instantly, I saw the folder with icon in an already open explorer window. Removed the flag again - folder looked default. Is this viable!?!? E.g. "dir" command does NOT show that folder any longer (by default). :P – Dreamspace President Nov 20 '18 at 12:29
  • 1
    I also used `SHChangeNotify()` and it's not reliable in Win10. Sometimes it works, sometimes not. It doesn't change it immediately but takes 1 minutes or so. – Elron Aug 26 '21 at 08:34
  • or you may use the nuget package[windows Icon Changer] which provides utility methods to deal with the problem [Disclaimer- I'm the author https://www.nuget.org/packages/Nakshatra.Windows.IconChanger/ – Nakshtra Oct 02 '22 at 10:01