Saturday, November 8, 2008

File Icon from File Extension

First I tried to find the icon by searching the registry. Like, for .txt files, go to ".txt" under HKEY_CLASSES_ROOT, then see what is written for "(Default)", in this case, "txtfile". Again find "txtfile" under HKEY_CLASSES_ROOT and look into the "DefaultIcon" subkey to find the filename and index of the icon for this type of files. Then extract the icon with Win32 API.

That's a lot of work and unfortunately, this approach does not work for all file extensions, because some extensions have custom IconHandlers (don't really know what that means though). Then I found a better and neater way to do it by using the SHGetFileInfo function. You just need to give the file extension and set the flags appropriately to get the job done. Don't forget to call DestroyIcon on the HICON after you are done with it.

No comments: