Click here to Skip to main content
15,860,972 members
Articles / Desktop Programming / Windows Forms

Extending Visual Studio Setup Project

Rate me:
Please Sign up or sign in to vote.
4.88/5 (66 votes)
16 Jan 2011Ms-PL4 min read 252.6K   9.8K   235   45
Extending Visual Studio Setup Project for Building Reliable Windows Installer (MSI)
1.png

Introduction

One of the things I hear all the time when companies talk about building reliable Windows Installer (MSI) for their product is –> Visual Studio Setup Project is not enough and we need much much more, my first response is – Wait, VS Setup project is not like Advanced Installer, InstallShield but still can do a lot more outside the box.

Setup projects are used to create Windows Installer (.msi) files, which are used to distribute your application for installation on another computer or Web server. There are two types of setup projects:

  • Standard setup projects create installers that install Windows applications on a target computer.
  • Web setup projects create installers that install Web applications on a Web server.

In this article, I’ll show how to extend your VS Setup Project and help to understand how VS Setup makes it easy for developers to build reliable Windows Installer (MSI) in Visual Studio. (All my screenshots will take from Visual Studio 2010.)

  1. Getting Started With VS Setup Project
  2. Adding New User Dialog and Deployments Conditions
  3. Run External Application during Setup

Getting Started

Open Visual Studio and create new Setup Project called – "DemoSetup", the Setup project item can be found under "Other Project Types"-> "Setup and Deployment" –> "Visual Studio Installer".

Also create a WPF application called – "DemoWpfApplication", we need some project to work with.

Two things need to be done in order to get the MSI ready for deployment.

  1. On the DemoSetup project, Add Project Output from DemoWPFApplication – Setup will automatically find all related dependencies.
  2. Modify Setup Project properties (See picture below)

The Version Property is very important for various MSI, in order to identify old installation and overwrite existing files with a newer version.

2.png

Adding New User Dialog and Deployments Conditions

Now, this is one of most common improvement users always want – Add an additional Dialog during the setup and add your own questions or input.
In this step, I’ll show how to add conditions and add a new user Dialog, here are the requirements: Add the following file (Dummy Files):

  • Blue.bmp,Red.bmp, Green.bmp
  • License.rtf
  • "Readme for 2000.txt", "Readme for Windows 7.txt"

1. First Let's Add Our Product License Agreement

Select the Setup Project and click "User Interface Editor" icon:

3.png

This will open a view showing all the dialogs the user will see during the Setup process.
Click "Add Dialog" and choose "License Agreement", after adding this dialog view dialog properties and select the License.rtf file.

4.png

2. Add Deployment by Condition

Add deployment condition for "Readme for 2000.txt", "Readme for Windows 7.txt", the propose is to copy each file only for specific operation system.
Select "Readme for 2000.txt" to see his properties, locate Condition and add this command –

WindowsBuild = 2195 or VersionNT = 500 

Now select "Readme for Windows 7.txt" and add this in the Condition property –

WindowsBuild >= 7100 or VersionNT = 601 

(More information on Operating System Property Values)
The condition will allow you to copy specific file depending on the user OS.

3. Add your User Choose Condition

This step will show you how to add your own questions (using Radio Buttons) and allow the user to define a Favorite Color and use User choose as condition for deployment.
Add a new Dialog and choose "RadioButton (3 buttons)" add fill the information as below:

5.png

To add the condition, you need to select each of the following files: Blue.bmp, Red.bmp, Green.bmp and add the proper value in the condition property as follow - FAVORITECOLOR="Blue", FAVORITECOLOR="Red" etc.

Build the setup project and run it, select Green in "Favorite Color" and the result should look like:

6.png

Run External Application during Setup

In this step, I’ll show how to run an external application before the actual install process using "Installer Class".
Create new WPF Application project called – "SetupHelper" and add additional Item of type "Installer Class" called – "MyInstallerHelper".
The InstallerClass will allow you to override the following events:

  • Rollback
  • Install
  • OnAfterInstall
  • Commit
  • OnAfterRollback
  • OnAfterUninstall
  • OnBeforeRollback
  • OnBeforeUninstall
  • OnCommitted
  • OnCommitting
  • Uninstall
  • OnBeforeInstall

First, add your code for "SetupHelper" WPF application, I’ve added code to show current processes and process title, but you can add whatever you want.
Now we need to add "SetupHelper" WPF app to our setup project, select the Setup Project and "Add Project Output" of SetupHelper.
Select the Setup Project and click on the "Custom Actions Editor" icon:

7.png

On the install, add new "Custom Action" and from the "Application Folder", pick "Primary output from SetupHelper (Active).
Now select the new Custom Action and in the "CustomActionData", add the following -

/Run=SetupHelper.exe /WaitForExit=true 

Back to MyInstallerHelper, override OnBeforeInstall and paste the code below in order to get the CustomActionData you wrote above:

C#
protected override void OnBeforeInstall(IDictionary savedState)
{
    try
    {
        base.OnBeforeInstall(savedState);
        FileInfo fileInfo = new FileInfo
		(System.Reflection.Assembly.GetExecutingAssembly().Location);
        //Take custom action data values
        string sProgram = Context.Parameters["Run"];
        sProgram = Path.Combine(fileInfo.DirectoryName, sProgram);
        Trace.WriteLine("Install sProgram= " + sProgram);
        OpenWithStartInfo(sProgram);
    }
    catch (Exception exc)
    {
        Context.LogMessage(exc.ToString());
        throw;
    }
}

OpenWithStartInfo will run SetupHelper application and will pause the setup process while the SetupHelper is open.

C#
void OpenWithStartInfo(string sProgram)
{
    ProcessStartInfo startInfo = new ProcessStartInfo(sProgram);
    startInfo.WindowStyle = ProcessWindowStyle.Normal;
    string[] ExcludeKeys = new string[] { "run", "WaitForExit" };
    startInfo.Arguments = ContextParametersToCommandArguments(Context, ExcludeKeys);
    Trace.WriteLine("run the program " + sProgram + startInfo.Arguments);
    Process p = Process.Start(startInfo);
    ShowWindow(p.MainWindowHandle, WindowShowStyle.Show); 	//otherwise it is 
							//not activated 
    SetForegroundWindow(p.MainWindowHandle);
    BringWindowToTop(p.MainWindowHandle); 	// Make sure the user will see 
					// the new window above of the setup.
    Trace.WriteLine("the program Responding= " + p.Responding);
    if ((Context.IsParameterTrue("WaitForExit")))
    {
        p.WaitForExit();// Have to hold the setup until the application is closed.
    }
}

ContextParametersToCommandArguments gets the CustomActionData arguments you added in the Custom Action.

C#
public static String ContextParametersToCommandArguments
		(InstallContext context, string[] ExcludeKeys)
{
    ExcludeKeys = ToLower(ExcludeKeys);
    StringBuilder sb = new StringBuilder();
    foreach (DictionaryEntry de in context.Parameters)
    {
        string sKey = (string)de.Key;
        bool bAdd = true;
        if (ExcludeKeys != null)
        {
            bAdd = (Array.IndexOf(ExcludeKeys, sKey.ToLower()) < 0);
        }
        if (bAdd)
        {
            AppendArgument(sb, sKey, (string)de.Value);
        }
    }
    return sb.ToString();
}

public static StringBuilder AppendArgument(StringBuilder sb, String Key, string value)
{
    sb.Append(" /");
    sb.Append(Key);
    //Note that if value is empty string, = sign is expected, e.g."/PORT="
    if (value != null)
    {
        sb.Append("=");
        sb.Append(value);
    }
    return sb;
}

#region "FS library methods"
public static string[] ToLower(string[] Strings)
{
    if (Strings != null)
    {
        for (int i = 0; i < Strings.Length; i++)
        {
            Strings[i] = Strings[i].ToLower();
        }
    }
    return Strings;
}
#endregion //"FS library methods"
#region "showWindow"

// http://pinvoke.net/default.aspx/user32.BringWindowToTop
[DllImport("user32.dll")]
static extern bool BringWindowToTop(IntPtr hWnd);

[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetForegroundWindow(IntPtr hWnd);

//from http://pinvoke.net/default.aspx/user32.SwitchToThisWindow 
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, WindowShowStyle nCmdShow);

/// <summary>Enumeration of the different ways of showing a window using 
/// ShowWindow</summary>
private enum WindowShowStyle : uint
{
    Hide = 0,
    ShowNormal = 1,
    ShowMinimized = 2,
    ShowMaximized = 3,
    Maximize = 3,
    ShowNormalNoActivate = 4,
    Show = 5,
    Minimize = 6,
    ShowMinNoActivate = 7,
    ShowNoActivate = 8,
    Restore = 9,
    ShowDefault = 10,
    ForceMinimized = 11
}
#endregion

Try It

Now Run the Setup project and you will notice that during the installation process, your Setup Helper will pop up and prevent the Setup from continuing until you will close the application.

8.png

After the setup is complete, you will see your setup output as below:

9.png

Summary

As you saw in this article, Visual Studio Setup Project can be extended a lot more and this can be good for many applications.

History

  • 16th January, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Architect Sela
Israel Israel
Shai Raiten is VS ALM MVP, currently working for Sela Group as a ALM senior consultant and trainer specializes in Microsoft technologies especially Team System and .NET technology. He is currently consulting in various enterprises in Israel, planning and analysis Load and performance problems using Team System, building Team System customizations and adjusts ALM processes for enterprises. Shai is known as one of the top Team System experts in Israel. He conducts lectures and workshops for developers\QA and enterprises who want to specialize in Team System.

My Blog: http://blogs.microsoft.co.il/blogs/shair/

Comments and Discussions

 
QuestionSetup project msi - custom action - external exe Pin
Member 139010058-Oct-18 6:29
Member 139010058-Oct-18 6:29 
QuestionHow to ceate a MSI installer for windows service project Pin
Member 1035269312-May-15 0:21
Member 1035269312-May-15 0:21 
QuestionHow to Print the License Agreement in the LA Dialog in a msi setup project ? Pin
Member 109184658-Jul-14 1:04
Member 109184658-Jul-14 1:04 
GeneralMy vote of 4 Pin
ridoy6-Jun-13 23:02
professionalridoy6-Jun-13 23:02 
GeneralMy vote of 5 Pin
npdev1327-May-13 21:11
npdev1327-May-13 21:11 
Questiongreat ! Pin
Wrangly3-Jan-13 1:55
Wrangly3-Jan-13 1:55 
QuestionTime Bomb Pin
jalindar.sabale17-Nov-12 23:58
jalindar.sabale17-Nov-12 23:58 
GeneralThanks alot! Pin
Thilinalees8-Oct-12 0:33
Thilinalees8-Oct-12 0:33 
GeneralMy vote of 4 Pin
hari1911321-Sep-12 6:26
hari1911321-Sep-12 6:26 
GeneralRe: My vote of 4 Pin
Shai Raiten29-Sep-12 20:35
Shai Raiten29-Sep-12 20:35 
QuestionOnBeforeInstall event Pin
sten lembo27-Aug-12 2:48
sten lembo27-Aug-12 2:48 
AnswerRe: OnBeforeInstall event Pin
Shai Raiten29-Sep-12 20:36
Shai Raiten29-Sep-12 20:36 
Questionadd custom dialog in maintenance dialog Pin
joehom13-Aug-12 20:50
joehom13-Aug-12 20:50 
QuestionAdding of DB script files. Pin
Mohammed Hameed14-Jun-12 2:43
professionalMohammed Hameed14-Jun-12 2:43 
AnswerRe: Adding of DB script files. Pin
Shai Raiten6-Jul-12 21:47
Shai Raiten6-Jul-12 21:47 
GeneralMy vote of 5 Pin
Mohammed Hameed14-Jun-12 2:42
professionalMohammed Hameed14-Jun-12 2:42 
GeneralMy vote of 5 Pin
Lukann1-May-12 9:45
Lukann1-May-12 9:45 
QuestionAmazing Pin
ak1624-Apr-12 4:19
ak1624-Apr-12 4:19 
Questionneed help Pin
Sebastian T Xavier19-Apr-12 3:31
Sebastian T Xavier19-Apr-12 3:31 
GeneralMy vote of 5 Pin
shanmuga priya8-Mar-12 19:21
shanmuga priya8-Mar-12 19:21 
QuestionA vote of 5 and a (hopefully) helpful link Pin
Don Kackman28-Feb-12 10:30
Don Kackman28-Feb-12 10:30 
GeneralMy vote of 5 Pin
purvapatel10-Feb-12 2:06
purvapatel10-Feb-12 2:06 
GeneralRe: My vote of 5 Pin
Shai Raiten29-Feb-12 3:20
Shai Raiten29-Feb-12 3:20 
QuestionHow to change startup info for the setup project Pin
Member 820215825-Nov-11 1:57
Member 820215825-Nov-11 1:57 
AnswerRe: How to change startup info for the setup project Pin
Sofigolu0117-Nov-14 4:09
Sofigolu0117-Nov-14 4:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.