Get File Icons from registry [VB.NET Class]

Discussion in 'Visual Basic [VB]' started by ManzZup, Apr 21, 2011.

  1. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    http://zontek.zzl.org
    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:
    '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
            Try
                '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)
                    Else
                        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.

    Usage:

    You can get the icon by calling the getIcon() function
    Code:
    Function getIcon(ByVal ext As String) As Icon
    
    Example:
    Code:
    Dim iconExtract As New getIconFrmReg
    Me.Icon = iconExtract.getIcon(".mp3")
    
    Used this in ma extractor:

    [​IMG]

    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.
     
    Last edited by a moderator: Jan 21, 2017
  2. jobsdeli

    jobsdeli New Member

    Joined:
    Jun 1, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.inetfresherjobs.com/
    is this working for any one
     
  3. ManzZup

    ManzZup New Member

    Joined:
    May 9, 2009
    Messages:
    278
    Likes Received:
    43
    Trophy Points:
    0
    Occupation:
    Production Manager:Software @ ZONTEK
    Location:
    Sri Lanka
    Home Page:
    http://zontek.zzl.org
    this is working and i have a working software made using this class
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice