PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[Registry/Windows] Performing simple file associations-VBForums
Results 1 to 1 of 1

Thread: [Registry/Windows] Performing simple file associations

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009

    [Registry/Windows] Performing simple file associations

    These two functions allow you to associate a program with a certain file type:

    Imports System.Runtime.InteropServices
    Main code:
    vb Code:
    1. <DllImport("shell32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    2.     Private Shared Sub SHChangeNotify(ByVal wEventId As UInt32, ByVal uFlags As UInt32, ByVal dwItem1 As IntPtr, ByVal dwItem2 As IntPtr)
    3.     End Sub
    4.     Private Const SHCNE_ASSOCCHANGED As UInt32 = &H8000000
    5.     Private Const SHCNF_IDLIST As UInt32 = &H0
    7.     Public Shared Function Associate(ByVal type As String, ByVal typename As String, ByVal executable As String, Optional ByVal arguments As String = "%1") As Boolean
    8.         DeAssociate(type)
    9.         Try
    10.             My.Computer.Registry.ClassesRoot.CreateSubKey("." & type).SetValue("", typename)
    11.             My.Computer.Registry.ClassesRoot.CreateSubKey(typename & "\shell\open\command").SetValue("", executable & " " & arguments)
    12.             My.Computer.Registry.ClassesRoot.CreateSubKey(typename & "\DefaultIcon").SetValue("", executable)
    13.             SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero)
    14.             Return True
    15.         Catch
    16.             Return False
    17.         End Try
    18.     End Function
    19.     Public Shared Function DeAssociate(ByVal type As String) As Boolean
    20.         Try
    21.             Dim typename As String = My.Computer.Registry.ClassesRoot.OpenSubKey("." & type).GetValue("")
    22.             My.Computer.Registry.ClassesRoot.DeleteSubKey("." & type)
    23.             My.Computer.Registry.ClassesRoot.DeleteSubKeyTree(typename)
    24.             SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, IntPtr.Zero, IntPtr.Zero)
    25.             Return True
    26.         Catch
    27.             Return False
    28.         End Try
    29.     End Function
    30.     Public Shared Function IsAssociated(ByVal type As String) As Boolean
    31.         Try
    32.             Dim typename As String = My.Computer.Registry.ClassesRoot.OpenSubKey("." & type, False).GetValue("")
    33.             My.Computer.Registry.ClassesRoot.OpenSubKey(typename, False)
    34.             Return True
    35.         Catch
    36.             Return False
    37.         End Try
    38.     End Function

    To associate:
    Associate("type", "Type file", "C:\program files\typeopener.exe", "-i %1"
    Associates the file type ".type" to the typeopener.exe program. When opening the file in Windows it will use the "-i %1" arguments, where "%1" is the file opened. The type displayed in the properties dialog will be "Type file".

    To de-associate:
    It will search the type name of "type" (="Type file") and removes both keys and for the type name the entire tree.

    To check if a type is associated:
    If IsAssociated("type") Then
         MessageBox.Show("Type is associated")
    End If
    Note that all these functions are not very solid, since they use a Try-Catch block to check if the operation is possible.
    You could add checks to existing keys, but looping the GetSubKeyNames function could mean a loss of performance.
    SHChangeNotify is used to make Windows update the association internally. (Icons)

    For a more advanced class you could use FileAssociation.vb from

    Special thanks go to Edgemeal.

Tags for this Thread

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