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



37 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. Custom applications are available in market become some applications are much expensive due to clients preferred to customized application design to reduced their operating cost. Now on Outsourcing you can hire application developer from all over the world.

    ReplyDelete
  9. please i want to create the database when i finish instaling the projet and i don't know what to write in the installer classe .
    can you help me please !!

    ReplyDelete
  10. Great Post. Just what looking for. Thank you very much..

    ReplyDelete
  11. Hello dear,

    I want put condition before installation of setup if this condition is verified and true then setup should run else not

    ReplyDelete
  12. Great post! I am see the programming coding and step by step execute the outputs.I am gather this coding more information. It's helpful for me my friend. Also great blog here with all of the valuable information you have.
    Dot Net Training in Chennai

    ReplyDelete
  13. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    Selenium Training in Chennai | Selenium Training in Bangalore | Selenium Training in Pune | Selenium online Training

    ReplyDelete
  14. Can you please explain The above process in C++

    ReplyDelete
  15. Jitendra sir .. hats off for your article

    ReplyDelete
  16. I'm getting mad about this issue, so thank you for sharing! I have implemented your solution and I see the message when installing, but not when uninstalling, and I need to perform some actions at uninstalling... Dou you have any idea why it does not work? Thank you in advance!

    ReplyDelete
  17. Nice to learn something different about this topic from this article. Thanks for your valuable information, it is very easy to learn and understand.

    AWS training in Chennai

    AWS Online Training in Chennai

    AWS training in Bangalore

    AWS training in Hyderabad

    AWS training in Coimbatore

    AWS training

    ReplyDelete
  18. its really interesting to read and useful article
    best-angular-training in chennai |

    ReplyDelete
  19. Nice blog thank you .For your Sharing It's a pleasure to read your post.It's full of information I'm looking for and I'd like to express that "The content of your post is awesome"


    Dotnet Training in Chennai
    Core java Training in Chennai

    ReplyDelete

  20. Nice blog thank you .For your Sharing It's a pleasure to read your post.It's full of information I'm looking for and I'd like to express that "The content of your post is awesome"
    ASP.NET Training in Chennai
    C#.NET Training In Chennai
    hadoop training in chennai
    software testing training in chennai
    ccna training in chennai

    ReplyDelete
  21. Whatsapp Number Call us Now! 01537587949
    outsourcing in Bangladesh
    USA pone web iphone repair USA
    USA SEX WEB careful
    bd sex video B tex
    bd sex video sex video
    bd sex video freelancing course

    ReplyDelete
  22. You should take help from professionals who have immense experience on Microsoft Business Central. They will help you with Solutions easily. Read: dynamics 365 business central pricing

    ReplyDelete