DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • The Technology Stack Needed To Build a Web3 Application
  • Cryptography and Secure Connection: How It Works
  • Deploy WordPress on AWS Using OpenEBS
  • Why Use LocalPV with NVMe for Your Workload?

Trending

  • Embracing Reactive Programming With Spring WebFlux
  • Programming With AI
  • Common Problems in Redux With React Native
  • Driving Digital Transformation Through the Cloud

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

Akhil Mittal user avatar by
Akhil Mittal
·
Jun. 03, 15 · Interview
Like (0)
Save
Tweet
Share
41.79K Views

Join the DZone community and get the full member experience.

Join For Free

introduction

there are 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 explaining to create a setup project, but some did not work, and some did not have a live example to follow.

the driving force for me to write this article is my qc team, who accept the main application for testing, and who also verified my setup installer with their 100% effort. and guess what, they successfully found 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 knowing that there are a number of other ways to do the same thing.

start the show

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

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

i gave the name creatinginstaller to my windows application, obviously you can choose your own.

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 however you want.

just wanted to write few lines of code, so i binded the button’s click event to show text box's text

primary objective

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

and add a setup project by other project types->setup and deployment->visual studio installer

the project will be added to the solution. now open the file system editor by clicking on the project and select the option to open file system editor, like in below figure:

you'll get to see application folder, user’s desktop and user’s program menu.

right click on application folder and add an output project. out project specifies the project we are creating an installer to, like in the following figure:

select creatinginstaller (i.e. the windows application project name) in the add output project window and select it as a primary output as shown below and click ok.

the primary output will be added as shown below, having type defined as output.

in the meanwhile, let's add some more functionality to our windows application. let's read a file and show its output in a message box upon a 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 a few lines of code just to read the txt file from the startup path. in my case bin\debug , it could also be bin\release as per the project build, and specify the file folder name and file name to read the content. i chose to keep my txt file at the 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 though it is unnecessary to do so.

therefore, running the application will show two message boxes, one after the other showing text box text and text from sample.txt file.

now this folder creation logic has to be implemented in our 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 .

right-click on folder, select properties, and mark the always create property to true. that means the folder will always be created whenever we run the installer, after a fresh build release.

create shortcuts

you can decorate your form to add an icon to it, and that icon will also be required at the time of installation to create a shotcut icon to our application. add an icon to the form like in below mentioned figure:

time to add the 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.

to create a shortcut to the application, right click on primary output in middle window pane and select create shortcut to primary output, 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 on the desktop when the application launches. the below figures explain how to add an icon.

cut the shortcut created at application folder and paste it under user’s desktop folder.

for shortcuts to be created at the user’s program menu, add a new folder to the user’s program menu. this will be created at the program’s menu location in that folder. create a new shortcut pointing to the primary output as we did when we created a desktop shortcut. the three images below describe the process:

name the folder creatinginstaller.

right click on middle window pane to create a new shortcut.

select shortcut source to primary output selected.

also add icon to shortcut, as done for desktop shortcut.

right click application folder to set the properties of where to install the application.

uninstall

we always have an option to uninstall the application from the control panel’s programs and features list, but how about creating our own uninstaller? that is also under the programs menu so we do not have to disturb the control panel.

step 1

right click on file system on target machine and add special folder->system folder as shown in below figure.

step 2

right click on the newly created system folder and browse for the msiexec.exe file in the local system.windows32 folder. this file takes responsibility to install and uninstall the application based on certain parameters specified.

set the properties of the file exactly as shown in the figure:

step 3

now create a new shortcut under the user’s program menu and point its source to msiexec as shown below. you can add more icons and a name to your shortcut. i have given it the name "uninstall."

step 4

press f4 key by selecting the setup project. we see a list of properties, which we can customize as per out installation needs, like product name, author, installation location. i’ll not go into a deep discussion about all of this, as they are quite easy to understand and set.

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

step 5

right click the uninstall shortcut and set the arguments property as shown in below figure:

hide copy code
/x {product code} /qr

/x is for uninstalltion.

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 whichever one you like.

step 6

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 an msi and a setup.exe . you can run either to initiate setup.

when we started we saw a setup wizard with screens that welcomed the user, asked for the location to install (while the default location was already set.)

after completing the wizard, click the close button.

now that the job is done we can see our shortcuts to the application created at desktop and user’s program menu like in below given figure.

now if we navigate out to the installation location we can also see the input folder created and the sample.txt file resting inside it.

run the application and see the output.

click on uninstall to remove the application. the wizard launches as shown below:

custom actions

just wanted to give a glimpse of custom actions we can define, while creating the setup.

custom actions are the actions which contain customized functionality apart from default one at the time of installation and uninstallation. for example, my qc team reported a bug that when running the application while simultaneously in background uninstalling the application, the application still keep on running. as per them it should show a message or close during the uninstallation. it was hard to explain to them the reason for this, so 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.

my need was to write code for the 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, it kills the process. not going into more details to it.just want to explain the use of custom actions.

using system;
using system.collections;
using system.collections.generic;
using system.componentmodel;
using system.configuration.install;
using system.diagnostics;
using system.linq;
namespace creatinginstaller
{
    [runinstaller(true)]
    public partial class installer1 : system.configuration.install.installer
    {
        public override void install(idictionary savedstate)
        {
            base.install(savedstate);
            //add custom code here
        }
        public override void rollback(idictionary savedstate)
        {
            base.rollback(savedstate);
            //add custom code here
        }

        public override void commit(idictionary savedstate)
        {
            base.commit(savedstate);
            //add custom code here
        }
        public override void uninstall(idictionary savedstate)
        {
            process application = null;
            foreach (var process in process.getprocesses())
            {
                if (!process.processname.tolower().contains("creatinginstaller")) continue;
                application = process;
                break;
            }

            if (application != null && application.responding)
            {
                application.kill();
                base.uninstall(savedstate);
            }
        }

    }
}
  1. click on the custom actions editor after selecting the creatinginstallersetup project.

  2. we see the custom action editor pane on left window. right click it to add a custom action and select the primary output in the application folder.

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

.net framework

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

right click on setup project, to open 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 that needs to follow, and select the radio button at number 2 (i.e. download prerequisites from the same location as my application.) press ok, but save the project and re-build it.

now when we browse the debug folder of the setup project we see two more folders as a result of the actions we performed just now.

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

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 the registry or license agreements though. there are many things to be explored 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!

please visit my blog a practical approach for more informative articles.


application File system

Opinions expressed by DZone contributors are their own.

Related

  • The Technology Stack Needed To Build a Web3 Application
  • Cryptography and Secure Connection: How It Works
  • Deploy WordPress on AWS Using OpenEBS
  • Why Use LocalPV with NVMe for Your Workload?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: