In .Net (C# or VB: don't care), given a file path string, FileInfo struct, or FileSystemInfo struct for a real existing file, how can I determine the icon(s) used by the shell (explorer) for that file?
I'm not currently planning to use this for anything, but I became curious about how to do it when looking at this question and I thought it would be useful to have archived here on SO.
This works for me in my projects, hope this helps someone.
It's C# with P/Invokes it will work so far on x86/x64 systems since WinXP.
(Shell.cs)
(NativeMethods.cs)
This link seems to have some info. It involves a lot of registry traversing, but it seems doable. The examples are in C++
"HKCR\.{extension}"
, read the default value (let's call itfiletype
)"HKCR\{filetype}\DefaultIcon"
, read the default value: this is the path to the icon file (or icon container file, like an .exe with an embedded icon resource)edit/moved up from the comments:
If the icon is in a container file (this is quite common), there will be a counter after the path, like this:
"foo.exe,3"
. This means it is icon number 4 (the index is zero-based) of the available icons. A value of ",0" is implicit (and optional). If the counter is 0 or missing, the fist available icon will be used by the shell.