martin2k

ForumsVB6GamesSoftwareDirectoryHTMLArticlesBlogContact

Extension Discoverer


 

Home

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

Contact


 

Program

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


vb_66_extdisc.zip
(5.04 KB)

This program uses the following ActiveX controls:

This project contains one Form, frmExtDisc:

frmExtDisc
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

frmExtDisc contains the following code:

Dim hKey As Long
Dim
value As String
Dim
length As Long
Dim
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
Else

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

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

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

RegCloseKey TopKey
End Function


Public Function FileTyp(File As String) As String
Dim
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

If
KeyCode = vbKeyReturn Then
    On Error GoTo
Eror

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

    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."
    Else
        FIcon.Text = Almost
    End If

    Kill CurFile
    Exit Sub

Eror:
    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.


blog comments powered by Disqus
Martin Allen 1999 - 2011.  Last updated Saturday 13 August 2011 12:13:07 AM +0100.