Creating an MSI Package for C# Windows Application Using a Visual Studio Setup Project



download_code Download Source Code

Introduction

There are a number of ways provided by Microsoft to create a setup project for windows application.

But when I started to create one, I got nothing but queries and confusions of how to start and where to start. There are numerous articles I found over the network, explaining to create a setup project, but some do not work as they say, and some do not have a live example to follow.

The driving force for me to write this article is my QC team, who except the main application to test also verified my setup installer with their 100% effort L. And guess what, they were successful to find bugs in that too.

In this article I would like to explain a step by step process to create a windows application and a setup installer for the same in a very simple manner, that is easy to understand and follow, alternatively, there are n no. of ways to do so.

Start the Show

Firstly let’s create a simple one form windows application, having a text box and a button only.

Creating windows application is just for the sake of having one to install.

clip_image002

I gave the name CreatingInstaller to my windows application, obviously, you can have your own.

Thus adding a new Windows Form Application in my solution and adding a text box and button to the default form resulted in the figure as shown below. Decorate the control properties as per your will.

clip_image004

Just wanted to write few lines of code, so I bound button’s click event to show text box’s text,

clip_image006

Primary Objective

So far so good. Now let’s create an installer for the same windows application. Right click on solution and add a new project to your solution like in the following figure,

clip_image008

And add a setup project by Other project Types->Setup and Deployment->Visual Studio Installer

clip_image010

The project will be added to the solution , now open file system editor by clicking on the project and selection the option to open file system editor like shown in below figure,

You’ll get to see Application Folder, User’s Desktop and User’s Program Menu.

clip_image012

Right click on Application Folder and add an output project , out project specifies for the project we are creating an installer to, like in following figure,

clip_image014

Select CreatingInstaller i.e. the windows application project name in Add output project window, select it as a primary output as shown below and click ok.

clip_image016

The Primary output will be added as shown below, having the type defined as Output.

clip_image018

In the meanwhile lets add some more functionality to our windows application, lets read a file and show its output in a message box on button click. Therefore, just add a text file, I called it Sample.txt to the bin\debug\Input folder, Input is the custom folder I created to place my txt file.

Write few lines of code just to read the txt file from Startup path, in my case bin\debug, it could also be bin\release as per project build, and specify the file folder name and file name to read the content.I chose to keep my txt file at startup path so that I could explain how we can create files and folders at the time of installation. Now we also need this Input folder and a Sample.txt file at the time of installation to be located at the location of installed application.

For file operations, I added the namespace System.IO, needless to specify this thou.

clip_image020

Therefore running the application will show two message boxes one after another showing text box text and text from Sample.txt file.

clip_image022

clip_image024

Now this folder creation logic has to be implemented in out setup project, so that when the application installs, it has all the pre-requisites required to run the application, like the Input folder and the Sample.txt file.

So, right click on Application Folder in File system editor and add a folder. The folder will be created just below the Application Folder, name that folder Input.

clip_image026

Right click on folder, select properties and mark Always Create property to True.That means folder will always be created whenever we run the installer, after a fresh build release.

clip_image028

Create Shortcuts

You can decorate your form to add an icon to it, that icon will also be required at the time of installation to create a shortcut icon to our application. Add an icon to the form like in below-mentioned figure,

clip_image030

Time to add Sample.txt file. Right click the Input folder created and Add file to it, browse for the Sample.txt file in the Windows Application project we created earlier.

clip_image032

To create a shortcut to the application, right click on Primary output in the middle window pane and select Create shortcut to Primary output, the name that shortcut as CreatingInstaller.

Select the properties of the shortcut, by right clicking it and add an icon to it. This icon will be created at the desktop when the application will launch. Below figures explain how to add an icon.

clip_image034

clip_image036

Cut the shortcut created at Application Folder and Paste it under User’s Desktop Folder.Job was done create a shortcut to user’s desktop.

clip_image038

For shortcuts to be created at User’s Program Menu, add a new folder to User’s Program Menu, that will be created at program’s menu location, in that folder create a new shortcut pointing the primary output as done for creating a desktop shortcut. The three images as following describe the process,

clip_image040

Name the folder CreatingInstaller.

Right click on the middle window pane to create a new shortcut.

clip_image042

Select shortcut source to primary output selected.

Also add an icon to shortcut, as done for Desktop shortcut.

clip_image044

Right click Application folder to set the properties of where to install the application,

clip_image046

Uninstall

We always have the option to uninstall the application from control panel’s Programs and Features list as simple as that but how about creating our own uninstaller, that too under programs menu so that we do not have to disturb control panel.

Step1:

Right click on File System on target Machine and Add Special Folder->System Folder as shown in below figure.

clip_image048

Step2:

Right click on newly created system folder and browse for the msiexec.exe file in local System.Windows32 folder. This file takes responsibility to install and uninstall the application based on certain parameters specified.

clip_image050

Set the properties of the file exactly as shown in the figure,

clip_image052

Step4:

Now create a new shortcut under User’s program Menu and point its source to msiexec as shown below. You can add icons and name to your shortcut. I have given it the name “Uninstall”.

clip_image054

Step5:

Press F4 key by selecting the setup project, we see a list of properties, we can customize these properties as per out installation needs, like set Product name, Author, Installation location, I’ll not go into a deep discussion about all this, as they are quite easy to understand and set.

Just take a note of product code shown below in the list of properties. We would need product code as a parameter to msiexec for uninstallation.

clip_image056

Step6:

Right click the Uninstall shortcut and set the arguments property as shown in below figure,

/x {product code} /qr

/x is for uninstallation.

You can get the whole detailed list of parameters and their use at http://technet.microsoft.com/en-us/library/cc759262(v=ws.10).aspx , chose the one you like to.

clip_image058

Step7:

Save all and Rebuild the setup project.

Job Done !

Now our setup is ready to install our windows application.

Just browse the debug folder location of Setup project, we find a msi and a setup.exe, one can run either of two to initiate setup.

When started we see a setup wizard, having screens which welcome’s user, which asks for location to install(already showing default location set.)

clip_image060

clip_image062

clip_image064

clip_image066

After completing the wizard,Click the close button.

Now Job is done, we can see our shortcuts to the application created at desktop and User’s Program Menu like in below-given figure.

clip_image068

Now if we navigate to out installation location we can also see the Input folder created and Sample.txt file resting inside it.

Run the application and see the output, works perfectly as was when executed from visual studio.

clip_image070

Click on uninstall to remove the application, the wizard launches as shown below,

clip_image072

Custom Actions

Just wanted to give a glimpse of Custom Actions we can define, while making setup.

Custom actions are the actions which contain customized functionality apart from default one at the time of installation and uninstallation. For e.g. My QC team reported a bug that when the run the application and in background uninstall the application, the application still keeps on running. As per them, it should show a message or close while uninstallation. Hard to explain them the reason, I opted for implementing their desire in the setup project.

1. Just add an installer class to the windows application we created earlier. When we open the installer class we can see the events specified for each custom action i.e. for Installation, Uninstallation, Rollback,Commit.

clip_image074

My need was to write code for uninstallation, so I wrote few lines to fulfill the need,

The code contains the logic to find the running exe name at the time of uninstallation, if it matches my application exe name, just kill the process. Not going into more details to it.Just want to explain the use of custom actions.

   1: using System;

 

   2: using System.Collections;

 

   3: using System.Collections.Generic;

 

   4: using System.ComponentModel;

 

   5: using System.Configuration.Install;

 

   6: using System.Diagnostics;

 

   7: using System.Linq;

 

   8: namespace CreatingInstaller

 

   9: {

 

  10: [RunInstaller(true)]

 

  11: public partial class Installer1 :

 

  12: System.Configuration.Install.Installer

 

  13: {

 

  14: public override void Install(IDictionary savedState)

 

  15: {

 

  16: base.Install(savedState);

 

  17: //Add custom code here 

 

  18: }

 

  19: public override void Rollback(IDictionary savedState)

 

  20: {

 

  21: base.Rollback(savedState);

 

  22: //Add custom code here 

 

  23: }

 

  24: public override void Commit(IDictionary savedState)

 

  25: {

 

  26: base.Commit(savedState);

 

  27: //Add custom code here 

 

  28: }

 

  29: public override void Uninstall(IDictionary

 

  30: savedState)

 

  31: {

 

  32: Process application = null;

 

  33: foreach (var process in Process.GetProcesses())

 

  34: {

 

  35: if

 

  36: (!process.ProcessName.ToLower().Contains("creatinginstaller"))

 

  37: continue;

 

  38: application = process;

 

  39: break;

 

  40: }

 

  41:

 

  42: if (application != null &&

 

  43: application.Responding)

 

  44: {

 

  45: application.Kill();

 

  46: base.Uninstall(savedState);

 

  47: }

 

  48: }

 

  49: }

 

  50: }

 

2. Click on Custom Actions Editor after selecting CreatingInstallerSetup project.

clip_image076

3. We see custom action editor pane on left window, right click it to add custom action and select primary output in Application Folder.

clip_image078

clip_image080

4. We see primary output added as custom actions now. Now at the time of uninstallation, my custom action will be fired, and the application will be closed while uninstalling it.

clip_image082

.Net Framework

What if installation machine does not have the .net framework. We can specify our own package supplied with installation, so that our application do not depend on the .net framework of a client machine, but points to the package we supplied to it to run.

Right click on Setup project, to open the properties window.

Here we can specify pre-requisites for the application to install. Just click on Prerequisites button and, in the opened prerequisites window, select the checkbox for the .net framework application needs to follow and select radio button at no. 2 i.e. Download prerequisites from the same location as my application. Press OK save the project and rebuild it.

clip_image084

Now when we browse to Debug folder of Setup project we see two more folders as a result of actions we did just now.

Now this whole package has to be supplied to the client machine for installation of the application.

clip_image086

Now re-install the application from setup.exe, and launch it using shortcuts.

Conclusion

 

The tutorial covers the basic steps for creating the installation project. I did not go very deep explaining registry, license agreements, though. There are many things to be explored yet to understand and master this topic. However, this article was just a start for a developer to play around with setup and deployments. Happy Coding Winking smile

Advertisements

Author: Akhil Mittal

Akhil Mittal is a Microsoft MVP,C# Corner MVP, Code project MVP, blogger, programmer by heart and currently working as a Sr. Analyst in Magic Software and have an experience of more than 9 years in C#.Net. He holds a B.Tech in Computer Science and hold a diploma in Information Security and Application Development. His work experience includes Development of Enterprise Applications using C#,.Net and Sql Server,Analysis, Product Management as well as Research and Development. He is a MCP in Web Applications(MCTS-70-528,MCTS-70-515) and .Net Framework 2.0 (MCTS-70-536).