Tuesday, November 21, 2006

Creating Custom Installer (msi)

I am writing this article since I found very less article on custom installer.

First create a dll (or an exe). Add an Installer class (UrInstaller.cs) into it.

It should generally contains :

  1. Install method
  2. Uninstall method

You can have an additional method that validates parameters (and if they are null, then assign default values).

In Install method, you can write custom action for setup. You can use the parameters by Context.Parameters["TARGETDIR"].

You cannot use any standard i/o methods eg. MessageBox or Console.WriteLine. But you can throw an exception and it will be caught by installer and displayed in message box.

Install method contains base.Install(stateSaver); by default.

Similarly you can write uninstall method for the removing any files you have created using custom install method.

If you face any error while using your custom Uninstall method, use Windows Installer Cleanup Utility (http://support.microsoft.com/kb/290301).

Then to the same solution, add a setup project. Add above project as primary project output.

Then in custom actions, Install or Uninstall > Application Folder > Primary output from above project.

Then right click > Property Window > Custom Action Data >
/someparameter=”[someparameter]” /targetdir=”[targetdir]/”

Note: targetdir is inbuilt parameter. Therefore extra backslash required.

You can access someparameters as Context.Parameters["someparameter"]

You can also add file or assembly and it will be placed in “targetdir”.

For installing the application, use
msiexec /i UrSetup.msi someparameter="abc"

For uninstalling the application, use
msiexec /x UrSetup.msi

Since, here parameters are not provided (this applies for Control Panel Uninstallation), you need to maintain an uninstall.log file which stores the parameters while install method.

No comments: