Results 1 to 7 of 7

Thread: VB - AssociateMyApp (File Association)

  1. #1

    Thread Starter
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    VB - AssociateMyApp (File Association)

    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

  2. #2
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: VB - AssociateMyApp (File Association)

    if you were specifying a default file extension, for example, .sdf and you wanted to use an icon called SDF.ico, how would you modify your code for this?

  3. #3

    Thread Starter
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: VB - AssociateMyApp (File Association)

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.   AssociateMyApp "SDF File Type", "C:\MyApp.exe %1", "sdf", , "C:\SDF.ico"
    5. End Sub

  4. #4
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: VB - AssociateMyApp (File Association)

    Quote Originally Posted by Aaron Young
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.   AssociateMyApp "SDF File Type", "C:\MyApp.exe %1", "sdf", , "C:\SDF.ico"
    5. End Sub
    thanks Mr. Young

  5. #5
    Fanatic Member drivenbywhat's Avatar
    Join Date
    Jan 2007
    Location
    VA - USA
    Posts
    866

    Re: VB - AssociateMyApp (File Association)

    Hey Aaron,

    Is there a way to unassociate an icon with your code? This option would be ideal for when a user doesn't want the program anymore.

  6. #6
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: VB - AssociateMyApp (File Association)

    Would it matter to much if I ran this code each time my app starts?

  7. #7
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: VB - AssociateMyApp (File Association)

    i tested on w7 (hprem)
    as limited user
    the code failed (without error) to write to HKCR

    running as limited user, run as administrator, the file association worked correctly and the command string was then processed correctly
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width