RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB
Expertise: Intermediate
Jun 24, 2008

Associate a File Extension to a Program

This tip is an addendum to the VB-2-the-Max tip CreateFileAssociation - Associate a file extension.

The following code shows the complete original tip. The addendum is in bold. Without the additional code, Windows puts a small icon inside a border (like in shortcuts).

Private Declare Sub SHChangeNotify Lib "shell32.dll" (ByVal wEventId As Long, _
    ByVal uFlags As Long, ByVal dwItem1 As Long, ByVal dwItem2 As Long)

' Create the new file association
' Extension is the extension to be registered (eg ".cad"
' ClassName is the name of the associated class (eg "CADDoc")
' Description is the textual description (eg "CAD Document"
' ExeProgram is the app that manages that extension (eg "c:\Cad\MyCad.exe")
' NOTE: requires CreateRegistryKey and SetRegistryValue functions

Sub CreateFileAssociation(ByVal Extension As String, ByVal ClassName As String, _
    ByVal Description As String, ByVal ExeProgram As String)
    Const HKEY_CLASSES_ROOT = &H80000000
    ' ensure that there is a leading dot
    If Left(Extension, 1) <> "." Then
        Extension = "." & Extension
    End If
    ' create a new registry key under HKEY_CLASSES_ROOT
    CreateRegistryKey HKEY_CLASSES_ROOT, Extension
    ' create a value for this key that contains the classname
    SetRegistryValue HKEY_CLASSES_ROOT, Extension, "", ClassName
    ' create a new key for the Class name
    CreateRegistryKey HKEY_CLASSES_ROOT, ClassName & "\Shell\Open\Command"
    ' set its value to the command line
    SetRegistryValue HKEY_CLASSES_ROOT, ClassName & "\Shell\Open\Command", "", _
        ExeProgram & " ""%1"""

    ' addendum by Andr
Andre Portugal
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date