Recently I was assigned the task to package the application contents by making an installer using Visual Studio. I was Googling and found some articles on it, however there are some more nuts and bolts that need to be tightened when you really start making an installer and distribute it to the client. Developers naturally upgrade their builds and then redistribute the application again. Here the Windows Installer does some magic by automatically detecting an old installed application and upgrading its files with the new version. Controlling this behavior is not allowed to a large extent but one can influence the code in a custom installer class and this code runs, once the application dumps the new files.
First of all I will quickly make a new new installer project and some project outputs in our installer project.
So for making a new installer project you have to do the following.
Step 1: Select a setup type project from the New Project dialog.
Now we will have a look at Solution Explorer of the setup project.
Here you can see the "Set up Sample" project has been added to the solution. On the top-most bar you can see some of the icons, each icon is worth opening to explore the functionality of the setup project. Additionally I have added 2 console apps, 1 window app and 1 class library just to show how to add multiple projects of various kinds to an installer setup. We can also choose which project(s) we want to add and which we don't want, since there can be some projects which are only for developers and testers.
I am going to explore some of them, since in the next step we will use these icons.
Step 2: Clicking on 'file system editor' icon
Adding project outputs to the installer: In simpler words we are now adding files which we actually want to install and these files should be project outputs ( exe, dlls etc). However there are more options available and one can add just a plain file, assembly etc. for the installer. All these files along with dependencies (referred files) will be installed on user system.
Step 3: Define target installation directory by setting 'Default location'
The project is ready as such and you can build it and distribute it. Now comes some advanced topices and tunings.
Advance Set up Options
What if you want to check some condition and conditionally rollback the installation?
Step 4: Add custom Actions, click on custom action icon
Custom actions are for adding code that is needed to be executed during installation.
Note: One important fact is that we can not run any code before installation starts, any code will run only after the installer dumps code files onto the user's system.
To add a custom action click on the custom action icon. This will open the following window and there we can add a project containing an installer class.
To create this project having an installer class, we can make a class library type of project and add an installer class in it from the add new item dialog.
From the above two images, you can see a class library project, "my installer" and an installer class in it.
To link this project to the installer project we add a "custom action" which is already shown before.
Further, to send a parameter from the installer project to the custom action class we can use a custom action property, "custom action data".
You can receive this data in a class using a context object as follows:
Step 5: Configuring automatic upgrades of new builds
Often we want to upgrade an old installed application with a new one in the user's systems and this should happen using the new setup file which we will distribute to users. That means the new installer file should automatically delete old files and upgrade new files. This is done by setting a few properties in the setup project as follows:
Remove Previous version: should be true to remove the previous version of your application.
Detect Newer Installed: Version should be true.
Version : will be changed whenever you want to make a new build. With change of version studio will ask you to change product code and you should click yes.
Upgrade Code: This should not be changed since it's common for all builds and .Net uses this code to detect old builds.
With this your setup should work and remove any older versions of applications. Although it is some times problematic particularly in Windows 7 where setup doesn't behave as expected, So additionally I tried to change the version for each project in my solution, and it worked for me.
Happy coding !!