Extension Discoverer



Read and add posts to the Visual Basic 6.0 Forums

Download some of my programs and OCXs

Read the Tips here

The old style forum is still available here




The Extension Discoverer program displays the icon and file-type for the file extension entered by the user.
(5.04 KB)

This program uses the following ActiveX controls:

This project contains one Form, frmExtDisc:


This form contains three textboxes - Ext, FType and FIcon.  FType and FIcon have their Locked property set to True and the BackColor property set to 'ButtonFace'.  There are three labels to the left of the TextBoxes and one Image control called IconPreview.

The project also contains a module called 'REG_MODULE' to allow the program to read from the registry.  This is necessary so that the program can lookup the icon filename and reference for the extension entered.  'REG_MODULE' (VBReg.bas) was written by Roger Wynn.


frmExtDisc contains the following code:

Dim hKey As Long
value As String
length As Long
value_type As Long

Public Function GetRegString(TopKey, Path As String, SValue As String) As String
length = 256
value = Space$(length)

If RegOpenKeyEx(TopKey, Path, 0&, KEY_ALL_ACCESS, hKey) <> ERROR_SUCCESS Then
    GetRegString = "[NOT FOUND]"
    Exit Function

    GetRegString = Left$(value, length - 1)
End If

RegQueryValueEx(hKey, SValue, 0&, value_type, ByVal value, length) <> ERROR_SUCCESS Then
    GetRegString = "[NOT FOUND]"
    Exit Function

    GetRegString = Left$(value, length - 1)
End If

RegCloseKey TopKey
End Function

Public Function FileTyp(File As String) As String
fs, f
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(File)
    FileTyp = f.Type
End Function

Private Sub Ext_KeyDown(KeyCode As Integer, Shift As Integer)
Dim CurFile As String, Almost As String

KeyCode = vbKeyReturn Then
    On Error GoTo

    CurFile = Environ("Temp") & "\Extdisc." & Ext.Text
    Open CurFile For Output As FreeFile

    Ext.SelStart = 0
    Ext.SelLength = Len(Ext.Text)
    FType.Text = FileTyp(CurFile)
    IconPreview.Picture = Allen.GetBigIcon(CurFile)
    Almost = GetRegString(HKEY_CLASSES_ROOT, "." & Ext.Text, "")
    Almost = GetRegString(HKEY_CLASSES_ROOT, Almost & "\DefaultIcon", "")

    If Almost = "[NOT FOUND]" Then
        FIcon.Text = "Extension does not exist or no icon has been assigned to this filetype."
        FIcon.Text = Almost
    End If

    Kill CurFile
    Exit Sub

    MsgBox "Illegal characters in extension (\ / : * ? " & Chr(34) & " < > |) or extension too long.", 16, "Error"
End If
End Sub

The GetRegString function reads a string from the specified location in the registry via the 'REG_MODULE' module.

FileTyp is used to find out the file type of the specified file.

When the user presses a key whilst the focus is on Ext TextBox, the Ext_KeyDown procedure is called.  The procedure tests to check whether the enter key was pressed.  If so, a file is created in the user's temp folder called Extdisc.<ext>, where <ext> is replaced with the extension entered in the Ext TextBox.

The FType TextBox is populated with the file type via the FileTyp function.  The 'HKEY_CLASSES_ROOT\.<ext>' part of the registry is read to get some information about the file type.  This information is used to read another part to get the filename of the icon.  This string is put into the FIcon TextBox.

An error message is displayed is the user enters illegal characters in the extension (\ / : * ? " & Chr(34) & " < > |) or if the extension is too long.

Martin Allen 1999 - 2011.  Last updated Saturday 13 August 2011 12:13:07 AM +0100.