0

I'm trying to get the associated icon for a file so I can display it in a ListView in my VB.Net code.

However, I can't use the ExtractAssociatedIcon function that all the Googling I've done points me to, because this is for a cataloguing function and the original file is not accessible, so all I have is the file path as a string (from which I can extract just the extension if necessary).

Is there a way I can get the default icon for a particular file extension, even if the file's unavailable?

Thanks

braX
  • 11,506
  • 5
  • 20
  • 33
Stuart Bruce
  • 111
  • 1
  • 7
  • 1
    Path of least resistance might be to create a file with a given extension in the temp folder and then pull the icon from there. Should never have a problem with the file/path being unavailable. This won't work for EXEs are the icon is generally custom and embedded in the EXE itself; there is a generic icon though. – UnhandledExcepSean Jun 12 '18 at 16:04
  • 1
    [This one](https://stackoverflow.com/q/2701263/2278086) is C#, but sounds like what you want. – Mark Jun 12 '18 at 16:07
  • 1
    You can use [AssocQueryString](https://msdn.microsoft.com/en-us/library/windows/desktop/bb773471(v=vs.85).aspx) to ask the Shell which Icon is associated with a specific file extension (except .exe files). See here [“Start Process” A File Without Extension](https://stackoverflow.com/questions/47762031/start-process-a-file-without-extension-in-vb-net?answertab=active#tab-top) how it works. You need to use the `AssocStr.DefaultIcon` enum to extract this information. It usually returns a .dll or .exe name and the internal icon index. Or directly an icon file path. – Jimi Jun 12 '18 at 21:50
  • Thanks everyone for the comments so far. The solution from UnhandledExcepSean is the one I've gone with as it was the least challenging to implement, and works very nicely with a very small amount of code. The SHGetFileInfo solution would probably have been more 'proper' but would've taken longer to get the result I was after. – Stuart Bruce Jun 14 '18 at 19:46

0 Answers0