Go4Expert (http://www.go4expert.com/)
-   Visual Basic [VB] (http://www.go4expert.com/articles/visual-basic-tutorials/)
-   -   Get File Icons from registry [VB.NET Class] (http://www.go4expert.com/articles/file-icons-registry-vbnet-class-t25586/)

ManzZup 21Apr2011 19:29

Get File Icons from registry [VB.NET Class]
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.

The Code

Class Name: getIconFrmReg.vb

Code: vb

'Retrieving the icon for an ext. from the registry
'@author: ManZzup
'@company: ZONTEK http://zontek.zzl.org
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
Code: vb

Function getIcon(ByVal ext As String) As Icon

Code: vb

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

Used this in ma extractor:

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 :D

Visit my blog for more.

jobsdeli 1Jun2011 15:30

Re: Get File Icons from registry [VB.NET Class]
is this working for any one

ManzZup 1Jun2011 16:51

Re: Get File Icons from registry [VB.NET Class]
this is working and i have a working software made using this class

All times are GMT +5.5. The time now is 19:29.