Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Building Custom Installer Classes in .NET : Page 2

When it comes to installations, automation beats manual intervention hands-down every time. Learn how to build custom installer classes to automate setup tasks that Visual Studio setup projects don't handle automatically.




Application Security Testing: An Integral Part of DevOps

Create the Installer Class
Create a class that inherits from the System.Configuration.Install.Installer class (you must first add a reference to the System.Configuration.Install.dll to your project). This class must be public, and it needs to be in one of the assemblies that are installed by your setup, so place it in one of your existing assemblies, or create a new assembly specifically for the custom installer class. Make sure you add its primary output to your setup. You must also add a RunInstallerAttribute(True) attribute to your installer class. For example:

<System.ComponentModel.RunInstallerAttribute(True)> _ Public Class MyInstaller Inherits System.Configuration.Install.Installer End Class

Override the Install and Uninstall Methods
You must override the Install and Uninstall methods to add the code to perform your custom installation steps. You can include user interaction when necessary; for example, your code can display forms to gather user input or display status and perform whatever custom steps are required for your application setup.

Make sure you call the base implementation of the Install and Uninstall methods within your overridden methods to ensure that your install executes automatic tasks such as logging to files and installing state recording. To do that call mybase.Install(stateSaver) and mybase.uninstall(savedState) respectively.

Be careful not to reference classes in your application assembles that are outside the assembly in which your installer class resides—they may not be installed yet!

Test Your Install Class
You run the installer utility installutil.exe (located in the .NET framework directory) from the command line to test your custom installer class. The utility accepts an assembly filename as an argument. You can also execute an uninstall using "/u" as the first command-line argument. The standard .NET framework 1.1 install places the installer utility on client machines, so you can also use installutil.exe to repeat setup tasks on client machines or to troubleshoot failed installations. For example, the following code fragment runs an install:

C:\Source Code\samples\bin>installutil installersample.dll

Or to run an uninstall, use:

C:\Source Code\samples\bin>installutil /u installersample.dll

Refer to the MSDN documentation for the installer utility for more information.

Custom Installer Class Example
Here's a simple example custom installer class that shows how to inherit from the Installer class and override the Install and Uninstall methods. This sample creates an event log source during the install process and removes it during the uninstall.

<System.ComponentModel.RunInstallerAttribute(True)> _ Public Class SimpleInstaller Inherits System.Configuration.Install.Installer Const EVENTSOURCE_NAME = "DevX Sample" Public Overrides Sub Install(ByVal stateSaver As _ System.Collections.IDictionary) Dim strAssemblyPath As String Dim frmSelection As New Forms.Selection Try MyBase.Install(stateSaver) EventLog.CreateEventSource( _ EVENTSOURCE_NAME, "Application") Catch exc As Exception MsgBox("Error performing installer tasks-" & exc.ToString, Exclamation, "Example") End Try End Sub Public Overrides Sub Uninstall(ByVal savedState _ As System.Collections.IDictionary) Try MyBase.Uninstall(savedState) EventLog.DeleteEventSource(EVENTSOURCE_NAME) Catch exc As Exception MsgBox("Error performing un-installer tasks-" & exc.ToString,Exclamation, "Example") End Try End Sub End Class

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date