This post is more of a permanent pointer so that I don’t have to trawl the web every time I want to find this information. One of the common tasks when building an installer for an desktop application is to add your own Custom Actions. These can be hooked up to the installer to execute at various points such as Before/After Install, Before/After Rollback and Before/After Uninstall. Each action can be written in managed code in a similar way to a typical event handler. The process goes a bit like this (assumes that you have already created your setup project and have added the primary output of your application):
- Add an Installer item to your application (from the New Item dialog select the Installer template)
- If the Installer item is not currently open, double-click it in the Solution Explorer to open the designer
- From the Properties grid select the Event view, followed by the event that you want to wire up (eg AfterInstall) – Double-clicking the drop-down box will create an empty event handler!
- Add your logic into the event handler that is created (this is your custom action)
- Right-click the setup project and select Custom Actions from the View menu
- Right-click the action (ie Install, Commit, Rollback or Uninstall) that you want to wire up (should correspond to the event you wrote the event handler for earlier) and select Add Custom Action (if you right-click the Custom Actions node it will wire up events for all four actions)
- From within the Application Folder select the Primary Output from your application (if it is not in this folder, or your installer is in a different class library you might need to add that using the Add File, Add Output or Add Assembly buttons).
These are the basic steps for getting your Custom Actions to execute as part of the installation process. Where this becomes a little tricky is where you want to prompt the user for some information during installation and then use this information as part of the custom action. For example, perhaps we are installing a service that needs to run as a particular user, we might want to configure this as part of the installation process.
- The first step is to create the user interface – Right-click the setup project and select User Interface
- Depending on where you want the dialog Right-click a node and select Add Dialog
- Select one of the pre-built dialogs (for example Textboxes (A))
- From the Properties grid configure the appropriate fields (eg set Edit1Label – “Specify the user to run service as:” and Edit1Property – “SERVICEUSER”) – this sets up the field so that user input is assigned to that property
- The next step is to pass this value into the Custom Action. To do this we need to specify the CustomActionData property so that this value is passed into the Installer. On the Custom Action view select the Installer and add the property information into the CustomActionData property using a /name=value sequence (eg “/name1=value1 /name2=value2”). In this case something like “/username=[SERVICEUSER]”
- Within the Installer this property can be accessed via the Context property – Me.Context.Parameters(“username”)
The last thing that you might want to do is to make this property configurable for when the installer is run from the command line in quiet mode. To do this you still need to pass in the command line argument into the Installer. For example I could run the installer using the following:
msiexec SERVICEUSER=”Fred” /i MyApplication.msi /quiet /passive
There is one catch which is you need to specify the Edit1Value=[SERVICEUSER] back on the User Interface view of the Installer. With that you are all done (well, excluding all the other mirad of properties you can tweak on your installer!)
Oh, it must be the time for writing posts on building installers because I just noticed that Dave Glover has posted on building Vista compatible installers for Windows Mobile applications.