The following code creates a simple function; AssociateMyApp which makes it easy to create/maintain a file association with your application, including:
  • Making your application the default for a file extension
  • Changing the Icon associated with the file extension
  • Setting up the various actions for the file association (Open by default)


Add the following to a Standard Module:
VB Code:
  1. Option Explicit
  2.  
  3. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  4. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
  5. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
  6. Private Declare Function SHChangeNotify Lib "shell32.dll" (ByVal wEventID As Long, ByVal uFlags As Long, ByVal dwItem1 As String, ByVal dwItems As String) As Long
  7.  
  8. Private Const HKEY_CLASSES_ROOT = &H80000000
  9. Private Const SHCNE_ASSOCCHANGED = &H8000000
  10. Private Const SHCNF_IDLIST = &H0
  11.  
  12. Public Sub AssociateMyApp(ByVal sAppName As String, ByVal sEXE As String, ByVal sExt As String, Optional ByVal sCommand As String, Optional ByVal sIcon As String)
  13.     Dim sCommandString As String
  14.     Dim lRegKey As Long
  15.    
  16.     'Open/Create the Extension under the "HKEY_CLASSES_ROOT" Hive of the Registry
  17.     Call RegCreateKey(HKEY_CLASSES_ROOT, "." & sExt, lRegKey)
  18.     'Set the "Default" value of the Key to the Application Name
  19.     Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sAppName, Len(sAppName))
  20.     'Close the Registry Key
  21.     Call RegCloseKey(lRegKey)
  22.    
  23.     sCommand = "\Shell\" & IIf(Len(sCommand), sCommand, "Open") & "\Command"
  24.     'Create the Application Key in the "HKEY_CLASSES_ROOT" Hive of the Registry
  25.     Call RegCreateKey(HKEY_CLASSES_ROOT, sAppName & sCommand, lRegKey)
  26.     'Set the Command to the EXE
  27.     Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sEXE, Len(sEXE))
  28.     'Close the Registry Key
  29.     Call RegCloseKey(lRegKey)
  30.     'If an Icon is required...
  31.     If Len(sIcon) Then
  32.        'Create a "DefaultIcon" entry under the Association Key
  33.         Call RegCreateKey(HKEY_CLASSES_ROOT, sAppName & "\DefaultIcon", lRegKey)
  34.         Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sIcon, Len(sIcon))
  35.         Call RegCloseKey(lRegKey)
  36.     End If
  37.     'Notify the Shell that an Association has Changed, (Updates Icons).
  38.     SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, vbNullString, vbNullString
  39. End Sub
Example Usage:
VB Code:
  1. Option Explicit
  2.  
  3. Private Sub Command1_Click()
  4.   AssociateMyApp "MyFileType", "C:\MyApp.exe %1", "xyz", , "C:\MyApp.exe,-1"
  5. End Sub