-
Apr 17th, 2003, 10:12 PM
#1
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:
Option Explicit
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
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
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const SHCNE_ASSOCCHANGED = &H8000000
Private Const SHCNF_IDLIST = &H0
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)
Dim sCommandString As String
Dim lRegKey As Long
'Open/Create the Extension under the "HKEY_CLASSES_ROOT" Hive of the Registry
Call RegCreateKey(HKEY_CLASSES_ROOT, "." & sExt, lRegKey)
'Set the "Default" value of the Key to the Application Name
Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sAppName, Len(sAppName))
'Close the Registry Key
Call RegCloseKey(lRegKey)
sCommand = "\Shell\" & IIf(Len(sCommand), sCommand, "Open") & "\Command"
'Create the Application Key in the "HKEY_CLASSES_ROOT" Hive of the Registry
Call RegCreateKey(HKEY_CLASSES_ROOT, sAppName & sCommand, lRegKey)
'Set the Command to the EXE
Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sEXE, Len(sEXE))
'Close the Registry Key
Call RegCloseKey(lRegKey)
'If an Icon is required...
If Len(sIcon) Then
'Create a "DefaultIcon" entry under the Association Key
Call RegCreateKey(HKEY_CLASSES_ROOT, sAppName & "\DefaultIcon", lRegKey)
Call RegSetValueEx(lRegKey, "", 0&, 1, ByVal sIcon, Len(sIcon))
Call RegCloseKey(lRegKey)
End If
'Notify the Shell that an Association has Changed, (Updates Icons).
SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, vbNullString, vbNullString
End Sub
Example Usage:
VB Code:
Option Explicit
Private Sub Command1_Click()
AssociateMyApp "MyFileType", "C:\MyApp.exe %1", "xyz", , "C:\MyApp.exe,-1"
End Sub
-
Jul 31st, 2006, 03:21 PM
#2
PowerPoster
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?
-
Jul 31st, 2006, 04:08 PM
#3
Re: VB - AssociateMyApp (File Association)
VB Code:
Option Explicit
Private Sub Command1_Click()
AssociateMyApp "SDF File Type", "C:\MyApp.exe %1", "sdf", , "C:\SDF.ico"
End Sub
-
Jul 31st, 2006, 04:43 PM
#4
PowerPoster
Re: VB - AssociateMyApp (File Association)
Originally Posted by Aaron Young
VB Code:
Option Explicit
Private Sub Command1_Click()
AssociateMyApp "SDF File Type", "C:\MyApp.exe %1", "sdf", , "C:\SDF.ico"
End Sub
thanks Mr. Young
-
Jan 11th, 2007, 02:45 PM
#5
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.
-
Mar 1st, 2007, 05:53 PM
#6
Fanatic Member
Re: VB - AssociateMyApp (File Association)
Would it matter to much if I ran this code each time my app starts?
-
Sep 6th, 2010, 07:03 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|