0

I have found plenty of examples in C#, but I cannot make this work in VB no matter what I try. The only icon I can extract is the one representing a file with no association. If there is a better approach I am open to that too. Here is the code:

Declaration:

Declare Auto Function ExtractAssociatedIcon Lib "shell32.dll" Alias "ExtractAssociatedIconA" (ByVal hInst As IntPtr, ByVal lpIconPat As String, ByRef lpiIcon As Integer) As IntPtr

Other Code:

Dim handle As IntPtr
Dim li As ListViewItem
Dim modul As System.Reflection.Module()

For Each filename As FileInfo In quotesFolder.GetFiles()

    If ImgLstQuotes.Images.ContainsKey(filename.Extension) Then
    Else
        modul = System.Reflection.Assembly.GetExecutingAssembly.GetModules()
        'handle = ExtractAssociatedIcon(Marshal.GetHINSTANCE(modul(0)), filename.FullName, -1) 'doesnt work

        'handle = ExtractAssociatedIcon(IntPtr.Zero(), filename.FullName, -1)  'doesn't work
        handle = ExtractAssociatedIcon(Process.GetCurrentProcess().Handle, filename.FullName, -1)  'doesn't work
        ImgLstQuotes.Images.Add(filename.Extension, Drawing.Icon.FromHandle(handle))
    End If

    li = LstVwQuotes.Items.Add(filename.Name, filename.Extension)
    li.Name = UCase(filename.Name)
    li.SubItems.Add(filename.LastWriteTime)

Next 

Thanks in advance!

theoldlr
  • 31
  • 3
  • Just use .NET Icon.ExtractAsssociatedIcon() method. Does the exact same thing without the bugs. – Hans Passant Nov 15 '13 at 00:40
  • @HansPassant I wish that was true but it states [here](http://msdn.microsoft.com/en-us/library/system.drawing.icon.extractassociatedicon(v=vs.110).aspx) that that will throw an exception with a UNC path. I changed the 3rd argument in the declaration to a short as you suggested (before editing your comment) but that did not work either. Any other thoughts much appreciated! – theoldlr Nov 15 '13 at 16:12

0 Answers0