Thursday, April 21, 2011

Get Icons of files from the registry [VB.NET Class]

helllooo everyone,
Just happen to see that i havent shared this, so i quickly wrote up this thing. This is a new class written by me to extract the icon assigned to a file in your current system.
Ex: Get the icon you see for mp3 files when you open your music folder
Many at first think this is just a WIN API wrapper for the SHGetfileinfo function. NOPE, that method would just grab the icon from an existing file, but if the files donot exist, say you make something like winzip and you need to show the icons of the file list inside, there's no way to use that method. So the only way is to retrieve it from the registry.

Class Name: getIconFrmReg.vb
'Retrieving the icon for an ext. from the registry
'@author: ManZzup
'@company: ZONTEK
Imports System.Runtime.InteropServices
Public Class getIconFrmReg
    Public Declare Function ExtractIconEx Lib "shell32.dll" (ByVal lpszFile As String, ByVal nIconIndex As IntPtr, ByRef phiconLarge As IntPtr, ByRef phiconSmall As IntPtr, ByVal nIcons As Integer) As IntPtr
    Dim regKey As String = My.Computer.Registry.ClassesRoot.ToString & "\"
    Shared Function getIconFromEx(ByVal file As String, ByVal index As Integer)
        Dim iconPtr As IntPtr
        Dim myicon As Icon
        ExtractIconEx(file, index, iconPtr, Nothing, 1)
        myicon = Icon.FromHandle(iconPtr)
        Return myicon
    End Function
    Function getIcon(ByVal ext As String) As Icon
        'Get the register value for the specified key
        Dim regVal As String = My.Computer.Registry.GetValue(regKey & ext, "", Nothing)
        Dim iconVal As String = ""
        Dim filen As String
        Dim nIndex As Integer
            'Specify the icon for .exe files
            If ext = ".exe" Then
                iconVal = "shell32.dll,2"

                'Specifi the common procedure
            ElseIf regVal <> "" Then
                If Not My.Computer.Registry.GetValue(regKey & regVal & "\DefaultIcon", "", Nothing) = Nothing Then
                    iconVal = My.Computer.Registry.GetValue(regKey & regVal & "\DefaultIcon", "", Nothing)
                    iconVal = "shell32.dll,0"
                End If

            ElseIf ext = "dir" Then
                iconVal = "shell32.dll,4"
            End If

            If iconVal.Length < 5 Then
                iconVal = "shell32.dll,0"
            End If
        Catch ex As Exception
            MsgBox("Error in icon")
        End Try

        Dim splt() As String = iconVal.Split(",")

        filen = splt(0).Trim(" ").Trim(Char.ConvertFromUtf32(34))
        nIndex = splt(1).Trim(" ")
        Dim icon As Icon
        icon = getIconFromEx(filen, nIndex)

        Return icon
    End Function
End Class

Make a new class named getIconFrmReg and paste this code.


You can get the icon by calling the getIcon() function

Function getIcon(ByVal ext As String) As Icon


Dim iconExtract As New getIconFrmReg
Me.Icon = iconExtract.getIcon(".mp3")

Used this in ma extractor:
[Image: icon.png]

That's it, and you'll get the icon returned, you can use this within imageLists or anything.
NOTE: This can extract icons in 98% of the cases but some rare cases are still being explored Big Grin


No comments:

Post a Comment