Results 1 to 6 of 6

Thread: VB Snippet - Create File Association

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    VB Snippet - Create File Association

    VB Code:
    1. 'Make a new project. Add a module. To the form add a command button.
    2.  
    3. 'Code:
    4. 'Add this code to the module:
    5.  
    6. Option Explicit
    7.  
    8. Public Const REG_SZ As Long = 1
    9. Public Const REG_DWORD As Long = 4
    10. Public Const HKEY_CLASSES_ROOT = &H80000000
    11. Public Const HKEY_CURRENT_USER = &H80000001
    12. Public Const HKEY_LOCAL_MACHINE = &H80000002
    13. Public Const HKEY_USERS = &H80000003
    14.  
    15. Public Const ERROR_NONE = 0
    16. Public Const ERROR_BADDB = 1
    17. Public Const ERROR_BADKEY = 2
    18. Public Const ERROR_CANTOPEN = 3
    19. Public Const ERROR_CANTREAD = 4
    20. Public Const ERROR_CANTWRITE = 5
    21. Public Const ERROR_OUTOFMEMORY = 6
    22. Public Const ERROR_INVALID_PARAMETER = 7
    23. Public Const ERROR_ACCESS_DENIED = 8
    24. Public Const ERROR_INVALID_PARAMETERS = 87
    25. Public Const ERROR_NO_MORE_ITEMS = 259
    26.  
    27. Public Const KEY_ALL_ACCESS = &H3F
    28. Public Const REG_OPTION_NON_VOLATILE = 0
    29.  
    30. Public Declare Function RegCloseKey Lib "advapi32.dll" _
    31.    (ByVal hKey As Long) As Long
    32.  
    33. Public Declare Function RegCreateKeyEx _
    34.     Lib "advapi32.dll" Alias "RegCreateKeyExA" _
    35.    (ByVal hKey As Long, _
    36.     ByVal lpSubKey As String, _
    37.     ByVal Reserved As Long, _
    38.     ByVal lpClass As String, _
    39.     ByVal dwOptions As Long, _
    40.     ByVal samDesired As Long, _
    41.     ByVal lpSecurityAttributes As Long, _
    42.     phkResult As Long, _
    43.     lpdwDisposition As Long) As Long
    44.  
    45. Public Declare Function RegOpenKeyEx _
    46.     Lib "advapi32.dll" Alias "RegOpenKeyExA" _
    47.    (ByVal hKey As Long, _
    48.     ByVal lpSubKey As String, _
    49.     ByVal ulOptions As Long, _
    50.     ByVal samDesired As Long, _
    51.     phkResult As Long) As Long
    52.  
    53. Public Declare Function RegSetValueExString _
    54.     Lib "advapi32.dll" Alias "RegSetValueExA" _
    55.    (ByVal hKey As Long, _
    56.     ByVal lpValueName As String, _
    57.     ByVal Reserved As Long, _
    58.     ByVal dwType As Long, _
    59.     ByVal lpValue As String, _
    60.     ByVal cbData As Long) As Long
    61.  
    62. Public Declare Function RegSetValueExLong _
    63.    Lib "advapi32.dll" Alias "RegSetValueExA" _
    64.   (ByVal hKey As Long, _
    65.    ByVal lpValueName As String, _
    66.    ByVal Reserved As Long, _
    67.    ByVal dwType As Long, _
    68.    lpValue As Long, _
    69.    ByVal cbData As Long) As Long
    70.  
    71.  
    72. Public Sub CreateAssociation()
    73.  
    74.    Dim sPath As String
    75.    
    76.   'File Associations begin with a listing
    77.   'of the default extension under HKEY_CLASSES_ROOT.
    78.   'So the first step is to create that
    79.   'root extension item
    80.    CreateNewKey ".xxx", HKEY_CLASSES_ROOT
    81.    
    82.    
    83.   'To the extension just added, add a
    84.   'subitem where the registry will look for
    85.   'commands relating to the .xxx extension
    86.   '("MyApp.Document"). Its type is String (REG_SZ)
    87.    SetKeyValue ".xxx", "", "MyApp.Document", REG_SZ
    88.    
    89.    
    90.   'Create the 'MyApp.Document' item under
    91.   'HKEY_CLASSES_ROOT. This is where you'll put
    92.   'the command line to execute or other shell
    93.   'statements necessary.
    94.    CreateNewKey "MyApp.Document\shell\open\command", HKEY_CLASSES_ROOT
    95.    
    96.    
    97.   'Set its default item to "MyApp Document".
    98.   'This is what is displayed in Explorer against
    99.   'for files with a xxx extension. Its type is
    100.   'String (REG_SZ)
    101.    SetKeyValue "MyApp.Document", "", "MyApp Document", REG_SZ
    102.    
    103.    
    104.   'Finally, add the path to myapp.exe
    105.   'Remember to add %1 as the final command
    106.   'parameter to assure the app opens the passed
    107.   'command line item.
    108.   '(results in '"c:\LongPathname\Myapp.exe %1")
    109.   'Again, its type is string.
    110.    sPath = "c:\LongPathname\Myapp.exe %1"
    111.    SetKeyValue "MyApp.Document\shell\open\command", "", sPath, REG_SZ
    112.    
    113.   'All done
    114.    MsgBox "The file association has been made!"
    115.    
    116. End Sub
    117.  
    118.  
    119. Public Function SetValueEx(ByVal hKey As Long, _
    120.                            sValueName As String, _
    121.                            lType As Long, _
    122.                            vValue As Variant) As Long
    123.  
    124.    Dim nValue As Long
    125.    Dim sValue As String
    126.    
    127.    Select Case lType
    128.       Case REG_SZ
    129.          sValue = vValue & Chr$(0)
    130.          SetValueEx = RegSetValueExString(hKey, _
    131.                                           sValueName, _
    132.                                           0&, _
    133.                                           lType, _
    134.                                           sValue, _
    135.                                           Len(sValue))
    136.          
    137.       Case REG_DWORD
    138.          nValue = vValue
    139.          SetValueEx = RegSetValueExLong(hKey, _
    140.                                         sValueName, _
    141.                                         0&, _
    142.                                         lType, _
    143.                                         nValue, _
    144.                                         4)
    145.    
    146.    End Select
    147.    
    148. End Function
    149.  
    150.  
    151. Public Sub CreateNewKey(sNewKeyName As String, _
    152.                         lPredefinedKey As Long)
    153.  
    154.   'handle to the new key
    155.    Dim hKey As Long
    156.  
    157.   'result of the RegCreateKeyEx function
    158.    Dim r As Long
    159.    
    160.    r = RegCreateKeyEx(lPredefinedKey, _
    161.                       sNewKeyName, 0&, _
    162.                       vbNullString, _
    163.                       REG_OPTION_NON_VOLATILE, _
    164.                       KEY_ALL_ACCESS, 0&, hKey, r)
    165.    
    166.    Call RegCloseKey(hKey)
    167.  
    168. End Sub
    169.  
    170.  
    171. Public Sub SetKeyValue(sKeyName As String, _
    172.                        sValueName As String, _
    173.                        vValueSetting As Variant, _
    174.                        lValueType As Long)
    175.  
    176.   'result of the SetValueEx function
    177.    Dim r As Long
    178.    
    179.   'handle of opened key
    180.    Dim hKey As Long
    181.    
    182.   'open the specified key
    183.    r = RegOpenKeyEx(HKEY_CLASSES_ROOT, _
    184.                     sKeyName, 0, _
    185.                     KEY_ALL_ACCESS, hKey)
    186.                    
    187.    r = SetValueEx(hKey, _
    188.                   sValueName, _
    189.                   lValueType, _
    190.                   vValueSetting)
    191.                  
    192.    Call RegCloseKey(hKey)
    193.  
    194. End Sub
    195.  
    196. 'Add this code to the command button:
    197.  
    198. Private Sub Command1_Click()
    199.  
    200. CreateAssociation
    201.  
    202. End Sub
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  2. #2
    Addicted Member TheAlchemist's Avatar
    Join Date
    Jan 2003
    Location
    Dar-esSalaam,Tanzania
    Posts
    139
    fantastic snippet mate. I really needed to learn how to do this. Thanks
    One thing that sustains me through life is the conciousness of the immense inferiority of everyone else
    --Oscar Wilde

  3. #3
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033
    You can add a Rebuild System Cache sub to refresh the system
    VB Code:
    1. Private Declare Sub SHChangeNotify Lib "shell32.dll" _
    2.            (ByVal wEventId As Long, _
    3.            ByVal uFlags As Long, _
    4.             dwItem1 As Any, _
    5.             dwItem2 As Any)
    6.  
    7. Public Sub RebuildSystemCache()
    8.  
    9.     SHChangeNotify SHCNE_ASSOCCHANGED, SHCNF_IDLIST, 0, 0
    10.  
    11. End Sub
    Help keep this forum clean: Remember to mark your thread as resolved · Search before you post · Remember to rate posts that help

    VS2010: Visual Studio 2010 Keybinding Posters
    · Service Pack 1
    Tools: GhostDoc - automatically generates XML documentation comments
    · NuGet package Manager · PowerCommands IDE extensions
    Source Control: ankhsvn - integration for SVN
    · Windows Shell Extension for Subversion

    Development Laptop: Intel Core i5 430M 2.26 GHz @ 2.53 GHz
    · 4096 MB, DDR3 PC3-8500F (533 MHz), Kingston · ATI Mobility Radeon HD 5470 · 15.6 @ 16:9, 1366x768 pixel, HD LED LCD

    I follow:
    JoelOnSoftware - A weblog by Joel Spolsky, a programmer working in New York City, about software and software companies
    ScottGu's Blog - Scott Guthrie works for Microsoft as the Product Manager of the .NET Framework
    Portugal-a-Programar - Portuguese Developers Community
    .NET Rocks! - is a weekly Internet audio talk show for .NET Developers.

    Programming Languages:
    C#
    · VB.NET · JAVA · PHP · Javascript
    Other:
    XML
    · HTML · CSS · JQuery · SQL



    *** Proudly Portuguese ***

  4. #4
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    How about assigning a protocol shortcut? Such as http://.

  5. #5
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349
    zuperman what is the values for the constants you've used??
    SHCNE_ASSOCCHANGED = ?
    SHCNF_IDLIST = ?
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  6. #6
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033
    Originally posted by Electroman
    zuperman what is the values for the constants you've used??
    SHCNE_ASSOCCHANGED = ?
    SHCNF_IDLIST = ?
    ups, forget about constants... here it goes...
    VB Code:
    1. Private Const SHCNE_ASSOCCHANGED = &H8000000
    2. Private Const SHCNF_IDLIST = &H0&
    Help keep this forum clean: Remember to mark your thread as resolved · Search before you post · Remember to rate posts that help

    VS2010: Visual Studio 2010 Keybinding Posters
    · Service Pack 1
    Tools: GhostDoc - automatically generates XML documentation comments
    · NuGet package Manager · PowerCommands IDE extensions
    Source Control: ankhsvn - integration for SVN
    · Windows Shell Extension for Subversion

    Development Laptop: Intel Core i5 430M 2.26 GHz @ 2.53 GHz
    · 4096 MB, DDR3 PC3-8500F (533 MHz), Kingston · ATI Mobility Radeon HD 5470 · 15.6 @ 16:9, 1366x768 pixel, HD LED LCD

    I follow:
    JoelOnSoftware - A weblog by Joel Spolsky, a programmer working in New York City, about software and software companies
    ScottGu's Blog - Scott Guthrie works for Microsoft as the Product Manager of the .NET Framework
    Portugal-a-Programar - Portuguese Developers Community
    .NET Rocks! - is a weekly Internet audio talk show for .NET Developers.

    Programming Languages:
    C#
    · VB.NET · JAVA · PHP · Javascript
    Other:
    XML
    · HTML · CSS · JQuery · SQL



    *** Proudly Portuguese ***

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