Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows
Visual Basic > Files Directories Drives sample source codes
Associate a file with a file extension
Associate a file with a file extension To associate a specific file with a file extension eg. "C:\Program Files\MS Office\Excel.exe" with ".xls", use the following routine. 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 RegCloseKey Lib "advapi32" (ByVal hKey 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 'Purpose : Creates a file association for a give file extension. 'Inputs : sAppExtension The file extension to associate. ' sApplicationPath The name of the file to open the specified files with. ' sDescription The description of the file type eg. "Excel Workbook". ' sIconPath The path to the file where the icon is stored. ' [sIconIndex] The index of the icon within the path. If not specified ' uses the first icon. 'Outputs : Returns True on success 'Notes : If updating an existing value, you may need to restart the computer before the ' changes take effect. ' Example usage: ' bResult = FileAssociationCreate(".txt", "notepad.exe", "A Notepad File") 'Revisions : Public Function FileAssociationCreate(sAppExtension As String, sApplicationPath As String, sDescription As String, Optional ByVal sIconPath As String, Optional sIconIndex As String = ",1") As Boolean Dim bResult As Boolean, sKeyName As String Const HKEY_CLASSES_ROOT = &H80000000 If Len(sIconPath) = 0 Then 'Use the application file for the icon sIconPath = sApplicationPath End If 'Write associations into registry sKeyName = Right$(sAppExtension, 3) & "file" bResult = zRegistryCreateKey(HKEY_CLASSES_ROOT, sAppExtension, sKeyName) bResult = bResult And zRegistryCreateKey(HKEY_CLASSES_ROOT, sKeyName & "\DefaultIcon", sIconPath & sIconIndex) bResult = bResult And zRegistryCreateKey(HKEY_CLASSES_ROOT, sKeyName, sDescription) bResult = bResult And zRegistryCreateKey(HKEY_CLASSES_ROOT, sKeyName & "\shell\open\command", sApplicationPath & " %1") FileAssociationCreate = bResult End Function 'Purpose : Creates a key or sets an existing keys value in the registry 'Inputs : lRootKey A constant specifying which part of the registry to ' write to, eg. HKEY_CLASSES_ROOT ' sRegPath The path to write the value of the key to. ' sValue The value of the key. 'Outputs : 'Notes : Used by FileAssociationCreate 'Revisions : Private Function zRegistryCreateKey(lRootKey As Long, sRegPath As String, sValue As String) As Boolean Dim lhwnKey As Long Dim lRetVal As Long Const REG_SZ = 1 On Error GoTo ErrFailed lRetVal = RegCreateKey(lRootKey, sRegPath, lhwnKey) If lRetVal = 0 Then 'Successfully created/opened the key 'Write value lRetVal = RegSetValueEx(lhwnKey, "", 0, REG_SZ, ByVal sValue, Len(sValue)) 'Close key lRetVal = RegCloseKey(lhwnKey) End If zRegistryCreateKey = (lRetVal = 0) Exit Function ErrFailed: zRegistryCreateKey = False End Function
Privacy Policy
|
Link to Us
|
Links