Listing the Registered File Extensions and Their Associated Icons

This code sample shows how to retrieve all the registered file extensions from the windows registry and how to extract the associated icon.

Screen-shot

The code is quite self explanatory and comes with a sample download.


option Explicit
'
'
'Aaron Young
'Analyst Programmer
'ajyoung@pressenter.com <mailto:ajyoung@pressenter.com>
'http://www.pressenter.com/~ajyoung
'

'Add a Listbox And a Picturebox to a Form..

private Declare Function RegCloseKey Lib "advapi32.dll" _
        (byval hKey as Long) as Long
private Declare Function RegEnumKey Lib "advapi32.dll" _
      Alias "RegEnumKeyA" (byval hKey as Long, _
      byval dwIndex as Long, byval lpName as string, _
      byval cbName as Long) as Long
private Declare Function RegOpenKey Lib "advapi32.dll" _
      Alias "RegOpenKeyA" (byval hKey as Long, byval lpSubKey as string, _
      phkResult as Long) as Long
private Declare Function RegQueryValueEx Lib "advapi32.dll" _
        Alias "RegQueryValueExA" (byval hKey as Long, byval lpValueName as string, _
        byval lpReserved as Long, lpType as Long, _
        lpData as Any, lpcbData as Long) as Long
private Declare Function DrawIconEx Lib "user32" _
        (byval hdc as Long, byval xLeft as Long, byval yTop as Long, _
        byval hIcon as Long, byval cxWidth as Long, byval cyWidth as Long, _
        byval istepIfAniCur as Long, byval hbrFlickerFreeDraw as Long, _
        byval diFlags as Long) as Long
private Declare Function ExtractIcon Lib "shell32.dll" _
        Alias "ExtractIconA" (byval hInst as Long, _
        byval lpszExeFileName as string, _
        byval nIconIndex as Long) as Long

private Const HKEY_CLASSES_ROOT = &H80000000

private aIcons() as string

private Sub Form_Load()
    Dim sType as string        'Ext.
    Dim sName as string        'Name of File Type
    Dim sFile as string        'File Used for Default Icon
    Dim iIndex as Integer
    Dim lRegKey as Long
    Dim iFoundCount as Integer

    List1.FontName = "Courier"
    iIndex = 1

    iFoundCount = 1

    sType = Space(255)
'Enumerate all Extensions in the CLASSES Hive..
    Do While RegEnumKey(HKEY_CLASSES_ROOT, iIndex, byval sType, 255) = 0
        If Left(sType, 1) <> "." then
        else

'Store Icon Info in an Array Linked by ListIndex
            ReDim Preserve aIcons(iIndex - 1)
            sType = Left(sType, InStr(sType, Chr(0)) - 1)
'get this Extensions Name, eg - .zip = WinZip
            If RegOpenKey(HKEY_CLASSES_ROOT, byval sType, _
               lRegKey) = 0 then
                sName = Space(255)
                Call RegQueryValueEx(lRegKey, byval "", 0&, _
                     1, byval sName, 255)
                If InStr(sName, Chr(0)) then sName = _
                   Left(sName, InStr(sName, Chr(0)) - 1)
                Call RegCloseKey(lRegKey)
                If len(Trim(sName)) then
'Look for a Default Icon for this Type..
                    If RegOpenKey(HKEY_CLASSES_ROOT, sName & _
                       "\DefaultIcon\", lRegKey) = 0 then
                        sFile = Space(255)
                        Call RegQueryValueEx(lRegKey, byval "", _
                             0&, 1, byval sFile, 255)
                        If InStr(sFile, Chr(0)) then sFile = _
                           Left(sFile, InStr(sFile, Chr(0)) - 1)
                        Call RegCloseKey(lRegKey)
                        aIcons(iFoundCount - 1) = sFile
                    End If
                End If

            End If
            List1.AddItem Left(sType & Space(10), 10) & " - " & sName
            iFoundCount = iFoundCount + 1

        End If

        sType = Space(255)
        iIndex = iIndex + 1
    Loop
End Sub

private Sub List1_Click()
    Dim sFile as string
    Dim iIndex as Integer
    Dim lIcon as Long

    Picture1.Cls
    on error GoTo IconErr
'get the Icon from the File Stored in the Array for this File Type
    sFile = Left$(aIcons(List1.ListIndex), _
            InStr(aIcons(List1.ListIndex), ",") - 1)
    iIndex = Val(mid$(aIcons(List1.ListIndex), _
             InStr(aIcons(List1.ListIndex), ",") + 1))
    lIcon = ExtractIcon(App.hInstance, sFile, iIndex)
    Call DrawIconEx(Picture1.hdc, 0, 0, lIcon, 32, 32, 0, 0, 3)
IconErr:
End Sub
'

Download Zipped Project File (5k)



Comments

  • Function

    Posted by Legacy on 05/21/2002 12:00am

    Originally posted by: Osiris

    Hey could someone please re-write this a function? I don't want to enumerate them all but call a function with an extension, and it set a label to the file type description, and add the icon to an imagelist. Thanks a bunch!

    Reply
  • Listing the Registered File Extensions

    Posted by Legacy on 03/14/2000 12:00am

    Originally posted by: Steve Hudson

    I have been trying to work out how to do exactly this. Great stuff!!

    • getting dlls from the registry

      Posted by mbnscs on 11/02/2004 12:21am

      i am tryng to get dlls from the registry and the set of dlls when any perticular action has occured by any user or application. so please help me in this regard.

      Reply
    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 6, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds