Vb.net – Items icons with listview and imagelist

imagelistlistviewvb.net

Hello I've got a list view that opens items inside a folder and displays them. I want to know if there is a way to have the list view display the icons aswell, maybe using shell32 or an imagelist. Here's the code:

Imports System.IO
Imports System.Xml
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic

Public Class cv7import

Private Sub cv7import_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim caminho As String
    caminho = "C:\Documents and Settings\Software\Ambiente de trabalho\1"



    lstvicon.View = View.Details
    lstvicon.GridLines = False
    lstvicon.FullRowSelect = True
    lstvicon.HideSelection = False
    lstvicon.MultiSelect = True


    lstvicon.Columns.Add("Nome")
    lstvicon.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)

    Dim DI As System.IO.DirectoryInfo = New System.IO.DirectoryInfo(caminho)

    Dim files() As System.IO.FileInfo = DI.GetFiles

    Dim file As System.IO.FileInfo

    Dim li As ListViewItem
    For Each file In files
        li = lstvicon.Items.Add(file.Name)
        li.Tag = file.FullName
    Next
End Sub


End Class

Here's two images, of how it looks and how I want it to look, if it helps.

How I wanted: http://imageshack.us/photo/my-images/21/wantd.png/

How it looks: http://imageshack.us/photo/my-images/13/needk.png/

Best Answer

Well you need to find the extension of the file to start with:

Dim file As String = "C:\scratch\newfile.txt"
Dim ext as string = IO.Path.GetExtension(file)

Then you need to find this entry in the HKEY_CLASSES_ROOT section of the registry:

HKEY_CLASSES_ROOT\.txt

The default value in this key gives the file type associated with this file in my case txtfile (remember that different registries may have different values depending on what the user has set up and what programs are installed)

You then need to look up this values DefaultIcon key in HKEY_CLASSES_ROOT:

HKEY_CLASSES_ROOT\txtfile\DefaultIcon

The default value in here gives you the location of the icon and the icon number in my case:

%SystemRoot%\system32\imageres.dll,-102

From this point I think you may need to rely on the ExtractIconEX API to extract the icon. This link may also be of use

Related Topic