Monday, October 15, 2012

How to add custom action to Windows Installer using C# ?


Visual Studio Package and Development wizard provides facility to create setup of your .Net Application. But in some cases we want to control the installation steps using custom action, means we want to add some custom functionality while installing the application.

Using custom action we can handle functionalities like-

-    Setting registry value while installation
-    Reading registry value while installation
-    Serial key implementation

Etc.

In this article, I am explaining - How to add custom action to Windows Installer in C#?

Here I am giving complete steps with pictorial representation. Follow these steps -

Step1: First, develop your application with required functionality.

Step2: For performing your custom action while installing application, we need to add "Installer Class" in application.  Add one "Installer Class" using "Add New Item" to the project like this-



Installer


Step 3: After adding Installer Class to the project , add your custom functionality , Here I am adding some code in "MyInstaller.cs" file-


//Code to perform at the time of installing application
public override void Install(System.Collections.IDictionary stateSaver)
        {
            System.Windows.Forms.MessageBox.Show("Installing Application...");
        }

//Code to perform at the time of uninstalling application
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            System.Windows.Forms.MessageBox.Show("Uninstalling Application...");
        }



Here I have added to events, this is for custom functionality, you can write your own code which you want to perform at the time of Installing and uninstalling application.

Step 4: Now set your application in "Release Mode" and build it.

Step 5: Now we have to add setup project for developed application, so right click on "Solution Explorer" and click on "Add -> New Project option".

Here you will get Project Templates dialog, Select "Visual Studio Installer" from "Other Project Types" option.

See this Image-


Setup


Step 6: After adding Setup project to solution, set project output for Setup project. For this right click on setup project and click on "Add -> Project Output"

you will get "Add project Output Group Dialog”, Here you need to set some setting like this-



Add Project Group


After setting click on "OK" button.

Step 7: Now to add custom action, again right click on "Setup project" and click on "View -> Custom Actions".


Custom Action



Now to set Custom action for installation right  click on "Install" click on "Add Custom Action",

Here double click on "Application folder" and select Primary output from Myproject and click on "OK" button.


Primary Output



Step 8: Repeat same for UN-installation process also.

Step 9: Finally your solution structure will be like this-


Solution Explorer

 

Step 10: Now build setup project and finally build the solution. Now you are done.

To test the complete process just try to install your application now, while installing application you will get message like this-



Installing Application



This is the same message which we have given in Install () event. Means whatever the action or code we will write in this event that will be perform at the time of installing application.

//Code to perform at the time of installing applicationpublic override void Install(System.Collections.IDictionary stateSaver)
        {
            System.Windows.Forms.MessageBox.Show("Installing Application...");
        }

And when you will uninstall application you will get message like this-


Uninstalling Application

 Here also we can see that the code which we have written in Uninstall () event that is executing.
Like Install () event we can handle Uninstall () event also.


//Code to perform at the time of uninstalling application
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            System.Windows.Forms.MessageBox.Show("Uninstalling Application...");
        }



These complete helps you to handle custom action while installation or uninstallation, Using these step we can implement serial key for application, means while installing application we can set expiration time for application and while uninstallation we can delete application related registry settings.



Thanks



20 comments:

  1. Before you install a release version of the .NET Framework, you must remove any beta versions on which the release version is based.

    .Net Application Development

    ReplyDelete
  2. I am reading a registry entry from HKCU hive in custom action, but it is not reading properly(coming as zero). Could you help with the same?

    ReplyDelete
  3. I am readying the all post. it is great and nice post. InstalleRex is a unique, smart and modern, cloud based installer, customized to your needs. Installerex makes cloud base installers ,Installerex expertise on the download monetization.InstalleRex build me a great and agile installer just the way i wanted , installerex custom installers

    ReplyDelete
  4. I really liked this post. Thank you so much for sharing this wonderful piece of work with us.

    installerex

    ReplyDelete
  5. great thing yar..I am trying to do this....nice.keep it up.

    ReplyDelete
  6. thanks a lot for this. helped a lot

    ReplyDelete
  7. Nice Tutorial :)
    But it seems like I have a problem every time i try to install gives me this error: "Error 1001. InstallUtilLib.dll: Unknown error".
    (Only occurs with custom action).
    Any help would be appreciated.

    ReplyDelete
    Replies
    1. Hi,

      Thanks for the appreciation...

      Check this link hope this will help you-

      http://social.msdn.microsoft.com/Forums/windows/en-US/1a1343ba-6991-4a69-8a2c-c0a0a23ead5b/error-1001-installutillibdll-unknown-error

      Delete
    2. This comment has been removed by the author.

      Delete
    3. Greetings
      Looks like i only could use .NET Framework 3.5 =/
      I tried with .Net 3.5 and its working without problems but in .Net 4.5 appears that annoying error :(
      Anyway I'll try to figure it out and if I find workaround i'll give the solution ;)

      P.S:
      I think that everybody should look to this tutorial after read this one :)

      Link -> http://blogs.msdn.com/b/wriju/archive/2009/05/21/setup-and-deployment-custom-action-to-capture-user-input.aspx

      I hopefully that you dont care for sharing this...
      And thanks for the quick answer =D

      Delete
  8. Acetech has many years of experience in custom software development. Find out more about custom software development at http://www.acetechindia.com

    ReplyDelete