Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

All about Installers - Customizing Windows and Web setup projects

0.00/5 (No votes)
10 Jan 2004 4  
This article guides how to customize an installer for specific needs of the application.

Introduction

The deployment is an essential part of the application life cycle. All that user wants from self-defined and self-controlled application is that it should be easy to deploy the application and undeploy without any hassles. This article mainly targets at how to customize the installers to satisfy the application needs. More often than not, Installers need customization to do pre/post processing during installation.

Visual Studio provides different ways to create installers. You can create installers for windows and web applications by creating set up or web set up projects. This article assumes that you need to customize an installer using VS set up projects. You will learn how to write an installer, and customize with different windows based on the need, pass parameters to the custom windows, commit/rollback based on the checks.

Installer Class

Creating an installer for the a windows or web project is a fairly simple job. All that you need to do is, open up your project for which you want to create installer, add a Setup and Deployment Project from File -> Add Project -> New Project. You can select the respective template from Templates window. The following code snippets describe based on a web setup project. The setup project contains few editors to based on their requirement. File System Editor is the default editor displayed, it provides interface to add files to the set up. There are two ways to add files to the set up project. Right clicking on Web Application Folder , it gives an option to add project output or file. Choosing Project Output, You get list of all projects in the solution. Select the project in concern, and add Primary output and Content Files. The second way to add files to the web setup project is selecting each file from the Web project and including them in same directory structure as of the web project.

If you want edit Registry during the installation, Registry Editor can be used to add registry keys under the respective node. User Interface Editor lists the different stages of installation and the dialog windows displayed during each stage. Figure 1 displays the User Interface Editor.

Figure 1 : User Interface Editor

The interesting part is adding custom dialog windows during the installation at particular stage. Right clicking on the Start/Progress/End node and select Add Dialog option. It shows list of predefined windows witch contain Radio Buttons/CheckBoxes/TextBoxes. Though there is not much control over these controls, but these windows allow to disable any particular control (EditVisible property) or provide a default value (EditValue property). The most important property is EditNProperty, using which the value of the control can be accessed when required (like [EditNProperty]). You can change the order of the windows appearing during the installation using move up/down options.

All these windows are predefined and most of the times, they do not fully provide the kind of functionality that most of the applications need. For example, If the installation has to take username/password as input, the default windows cannot be changed to accept password characters. There is an alternative to customize the installation for these kinds of requirements. This can be achieved using Custom Actions Editor. Custom Actions Editor displays four nodes (Install/Commit/Rollback/UnInstall). A custom action can be any action that does some task like running scripts, showing windows or any configuration that needs to be done during the installation. To add custom action, right click on the node, select add custom action option, select any dll/exe file. To make the selected dll/exe act as custom action, we need to make it as an installer as shown below.

[RunInstaller(true)]
public class WebInstaller : System.Configuration.Install.Installer {}

The Run time identifies a class as an installer based on the RunInstaller Attribute (System.ComponentModel). The Installer class that you write needs to be inherited from Installer class in System.configuration.Install namespace. The run time calls Install method of installer class during the installation. This method acts as a starting point for the custom action. To pass parameters to the action, CustomActionData property for the custom action needs to be set with required parameters. The following line shows how to pass virtual directory path and port number to custom action.

Custom Action Data = /VDIR=[TARGETVDIR] /PORT=[TARGETPORT]   /// Note

            //: each parameter needs to separated by a space.

Accessing parameters in Custom Action : Context.Parameters["VDir"] /// Context.Parameters is the collection of the parameters passed.

If custom action fails throw an instance of InstallException. This exception will inform the runtime to rollback the installation.

if (Context.Parameters["VDir"] == null)
{
    throw new InstallException("Virtual directory path not specified");
}

To provide custom dialog windows, add the particular EXE as custom action. The code attached with this article has an exe as custom action. This exe shows a window prompting username/password and tries to connect to sql server. It also updates web.config file with the connectionstring prepared using user input. It also has some cool code to update IIS to turn off anonymous option for the current virtual directory.

Using the code

The demo zip contains a customized installer. It installs web project creating a virtual directory. After installation, it asks for information to update config file and also updates IIS.

The Source zip contains CustomInstall web project, IISInterface project to update IIS, WebSetup project.

Summary

This article caters to the basic requirements of how to customize the installer to show more windows and implement pre/post checks. The key points are creating Custom Actions, passing data to them, aborting installation upon error. Along with these, article also shows how to update config file and IIS settings programmatically.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here