Dynamic Registry Installer Class

I wrote some simple yet effective code last night. Since I can't put it in the Interscape Base Library, I'm making it available here. Have at it:

Imports System.ComponentModel
Imports
System.Configuration.Install
Imports Microsoft.Win32

Namespace Installers

    <RunInstaller(
True
)> _
    Public Class
Installer
       
Inherits
System.Configuration.Install.Installer

        #Region " Component Designer generated code "

        Public Sub New()
            MyBase
.New()
           
'This call is required by the Component Designer.
           
InitializeComponent()
           
'Add any initialization after the InitializeComponent() call
       
End
Sub

       
'Installer overrides dispose to clean up the component list.
       
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean
)
           
If disposing
Then
               
If Not (components Is Nothing)
Then
                   
components.Dispose()
                End
If
           
End
If
           
MyBase
.Dispose(disposing)
       
End
Sub

       
'Required by the Component Designer
       
Private components As
System.ComponentModel.IContainer
       
'NOTE: The following procedure is required by the Component Designer
       
'It can be modified using the Component Designer.
       
'Do not modify it using the code editor.
       
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
            components =
New
System.ComponentModel.Container
       
End Sub

        #End Region

       
Public Overrides Sub Install(ByVal stateSaver As
System.Collections.IDictionary)
           
MyBase
.Install(stateSaver)
            
' Gets the parameter passed across in the CustomActionData.
           
Dim KeyName As String = Me
.Context.Parameters.Item("Key")
           
Dim Location As String = Me
.Context.Parameters.Item("Location")

           
If KeyName = "" Then Throw New
InstallException("The Registry key was not specified. The Registry could not be modified.")
           
If Location = "" Then Throw New
InstallException("The Path was not specified. The Registry could not be modified.")
       
           
Dim regKey As
RegistryKey
            regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\.NETFramework\AssemblyFolders",
True
)
            regKey.CreateSubKey(KeyName)
            regKey.SetValue("", Location)
            regKey.Close()
       
End
Sub

       
Public Overrides Sub Uninstall(ByVal stateSaver As
System.Collections.IDictionary)
           
MyBase
.Uninstall(stateSaver)
           
' Gets the parameter passed across in the CustomActionData.
           
Dim KeyName As String = Me
.Context.Parameters.Item("Key")
           
Dim Location As String = Me.Context.Parameters.Item("Location")

           
If KeyName = "" Then Throw New InstallException("The Registry key was not specified. The Registry could not be modified.")
           
If Location = "" Then Throw New InstallException("The Path was not specified. The Registry could not be modified.")

           
Dim regKey As RegistryKey
           
regKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\.NETFramework\AssemblyFolders", True)
           
regKey.DeleteSubKey(KeyName)
           
regKey.Close()

       
End Sub

   
End Class

End
Namespace

Now, all you do after this is add your assembly to the custom actions, 2 times for the Install and the Uninstall (4 total), and set the custom data to read: /Key=Interscape.1.0.3300.0 /Location=[TARGETDIR]\v2.0\Redistributable\For .NET 1.0 and /Key=Interscape.1.0.5000.0 /Location=[TARGETDIR]\v2.0\Redistributable\For .NET 1.1 respectively. Those examples are from Interscape's Base Library, of course, but you can see how it would work. Do the same thing for the Uninstall routine, and you're golden.

I think I need to build a tool for writing installer project templates...

1 Comment

  • &gt; Now, all you do after this is add your assembly

    &gt; to the custom actions, 2 times for the Install

    &gt; and the Uninstall (4 total)

    And how would i do that :S

Comments have been disabled for this content.