Often you need to get access to variables created during the installation process. One of the most common being the installation path or 'TARGETDIR'. This can be done by performing two steps.
Step one
In your deployment project, press the right mouse button on the project in the class view and select Custom Actions. You should see the four default states for the installer. Select the primary output for the Commit state.
In the properties window there is a property named CustomActionData, here you use name value pairs to define data to be accessible in your program. Setting the following property creates a variable named DIR with the target folder within it (Remember to include quotes as the path may contain spaces). This example also includes a trailing slash.
/DIR="[TARGETDIR]"
You can add additional variables by separating the values with commas.
Step two
In order to access the variable and do something during the installation you need to create a class which inherits from System.Configuration.Install.Installer. The following is a class written in vb.net which extends the commit of the installer to save a record in the registry of the target directory the app was installed to.
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.Windows.Forms
Imports Microsoft.Win32
<RunInstaller(True)> Public Class ProjectInstaller
Inherits System.Configuration.Install.Installer
Dim strInstallLocation As String
Public Overrides Sub Commit(ByVal savedState As IDictionary)
MyBase.Commit(savedState)
'get the install location
strInstallLocation = Me.Context.Parameters.Item("DIR")
If strInstallLocation = Nothing Then
strInstallLocation = String.Empty
Else
'always set the install dir
SetReportSettingsValue("InstallDir", strInstallLocation)
End If
End Try
End Sub
'default install function
Public Overrides Sub Install(ByVal savedState As IDictionary)
MyBase.Install(savedState)
End Sub
'function to set a registry value
Public Shared Function SetReportSettingsValue(ByVal Key As String, ByVal Value As String)
Dim RegKey As RegistryKey = Registry.LocalMachine
RegKey = RegKey.OpenSubKey("SOFTWAREReportSettings", True)
If Not RegKey Is Nothing Then
RegKey.SetValue(Key, Value)
RegKey.Close()
End If
End Function
End Class
Heres some other useful urls on the subject.
Installer for a Windows Service
Exploiting .NET's Advanced Deployment Features
|