Click here to Skip to main content
11,413,961 members (77,474 online)
Click here to Skip to main content

Launching Your Application After Install using Visual Studio 2005

, 11 Jul 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Launching your application after install using Visual Studio 2005

Introduction

Creating an install MSI file is rather simple; however there is very little documentation about it, or it's choppy and sometimes too wordy. Well, the point of this article is to show you how to create the installer application, and how to make it launch your application after it has finished installing.

Using the Code

The first thing you must do is add a new reference to your main project file.

Screenshot - 1.png

Add System.Configuration.Install to your project, for some reason this has not been added automatically.

Screenshot - 2.png

Now you must add an installer class to your main project. To do this, right click on that project, go to Add, and go to New Item…

Screenshot - 3.png

Name it Installer Class.

Screenshot - 4.png

Copy and paste the following code into your application. Be sure to change the application name to that of your main project, in my case it is InstallSample.exe.

VB.NET

Imports System
Imports System.Diagnostics
Imports System.Windows.Forms
Imports System.Collections
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.Reflection
Imports System.IO

Namespace OffLine.Installer
    ' Taken from:http://msdn2.microsoft.com/en-us/library/
    ' system.configuration.configurationmanager.aspx
    ' Set 'RunInstaller' attribute to true.

    <RunInstaller(True)> _
    Public Class InstallerClass
        Inherits System.Configuration.Install.Installer
        Public Sub New()
            MyBase.New()
            AddHandler Me.Committed, AddressOf MyInstaller_Committed
                ' Attach the 'Committed' event.
                ' Attach the 'Committing' event.
            AddHandler Me.Committing, AddressOf MyInstaller_Committing
        End Sub

        ' Event handler for 'Committing' event.
        Private Sub MyInstaller_Committing_
		(ByVal sender As Object, ByVal e As InstallEventArgs)
            'Console.WriteLine("");
            'Console.WriteLine("Committing Event occurred.");
            'Console.WriteLine("");
        End Sub

        ' Event handler for 'Committed' event.
        Private Sub MyInstaller_Committed(ByVal sender As Object, _
		ByVal e As InstallEventArgs)
            Try
                Directory.SetCurrentDirectory_
		(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location))
                Process.Start(Path.GetDirectoryName_
		(Assembly.GetExecutingAssembly().Location) + "\InstallSample.exe")
                    ' Do nothing... 
            Catch
            End Try
        End Sub

        ' Override the 'Install' method.
        Public Overloads Overrides Sub Install(ByVal savedState As IDictionary)
            MyBase.Install(savedState)
        End Sub

        ' Override the 'Commit' method.
        Public Overloads Overrides Sub Commit(ByVal savedState As IDictionary)
            MyBase.Commit(savedState)
        End Sub

        ' Override the 'Rollback' method.
        Public Overloads Overrides Sub Rollback(ByVal savedState As IDictionary)
            MyBase.Rollback(savedState)
        End Sub
    End Class
End Namespace

C#.NET

using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Reflection;
using System.IO;

namespace OffLine.Installer
{
  // Taken from:http://msdn2.microsoft.com/en-us/library/
  // system.configuration.configurationmanager.aspx
  // Set 'RunInstaller' attribute to true.

  [RunInstaller(true)]
  public class InstallerClass : System.Configuration.Install.Installer
  {
    public InstallerClass()
      : base()
    {
      // Attach the 'Committed' event.
      this.Committed += new InstallEventHandler(MyInstaller_Committed);
      // Attach the 'Committing' event.
      this.Committing += new InstallEventHandler(MyInstaller_Committing);
    }

    // Event handler for 'Committing' event.
    private void MyInstaller_Committing(object sender, InstallEventArgs e)
    {
      //Console.WriteLine("");
      //Console.WriteLine("Committing Event occurred.");
      //Console.WriteLine("");
    }

    // Event handler for 'Committed' event.
    private void MyInstaller_Committed(object sender, InstallEventArgs e)
    {
      try
      {
        Directory.SetCurrentDirectory(Path.GetDirectoryName
		(Assembly.GetExecutingAssembly().Location));
        Process.Start(Path.GetDirectoryName(
          Assembly.GetExecutingAssembly().Location) + "\\InstallSample.exe");
      }
      catch
      {
        // Do nothing... 
      }
    }

    // Override the 'Install' method.
    public override void Install(IDictionary savedState)
    {
      base.Install(savedState);
    }

    // Override the 'Commit' method.
    public override void Commit(IDictionary savedState)
    {
      base.Commit(savedState);
    }

    // Override the 'Rollback' method.
    public override void Rollback(IDictionary savedState)
    {
      base.Rollback(savedState);
    }
  }
}

After changing the project EXE file in the source code that you just pasted, be sure to recompile that project.

As you can see from the code, you added a new interface to the class that allows you to take control of the Install, Commit, Committed, and Rollback. In Committed, I am going to launch the InstallSample.exe application.

When it is time to create your install project, you will want to add a new Setup Project to your existing applications solution.

Screenshot - 5.png

Right click on this new project, then click Add, then go to Project Output…

Screenshot - 6.png

Select Primary Output, then click OK.

Screenshot - 7.png

Add a shortcut in the Start Menu… Right click on the Setup Project, then go to View, and then File System…

Screenshot - 8.png

Click on Application Folder, then right click the Primary Output file, and then go to Create Shortcut to Primary Output from InstallExample (Active):

Screenshot - 9.png

Add a new folder to your start menu, right click on User's Programs Menu, then go to Add, and then Folder. Rename that folder to Installer Example.

Screenshot - 10.png

Now drag that shortcut you created in the Applications Folder, to the new folder you just created:

Screenshot - 11.png

Right click on the new solution; go to View, then Custom Actions:

Screenshot - 12.png

Add a new custom action to the root Custom Actions node:

Screenshot - 13.png

Double click Applications Folder:

Screenshot - 14.png

Then click Primary output from InstallExample (Active):

Screenshot - 15.png

Lastly, rebuild your install solution.

Screenshot - 16.png

It is a good idea to keep an eye on the status bar, it will tell you when it is done…

Screenshot - 17.png

Now you can test it and it should install the application. After the install process, it should run automatically.

History

  • 11th July, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Bill Daugherty II

United States United States
No Biography provided

Comments and Discussions

 
Questionmy 5 Pin
Shambhoo kumar, 20-Jul-13 0:15
professionalShambhoo kumar20-Jul-13 0:15 
QuestionGood Post. Pin
Ranish kumar, 1-Nov-12 2:33
memberRanish kumar1-Nov-12 2:33 
QuestionThank you! Pin
Ehud Grand, 12-Jan-12 1:48
memberEhud Grand12-Jan-12 1:48 
QuestionThanks Pin
qhuydtvtv, 27-Dec-11 0:45
memberqhuydtvtv27-Dec-11 0:45 
GeneralMy vote of 5 Pin
lopezatienza, 15-Nov-11 3:31
memberlopezatienza15-Nov-11 3:31 
Questionto solve problem "can not find $path\projectname.intallstate" Pin
hacklew, 16-Aug-11 17:05
memberhacklew16-Aug-11 17:05 
GeneralMy vote of 5 Pin
EAlger, 25-May-11 11:21
memberEAlger25-May-11 11:21 
GeneralMy vote of 5 Pin
jonty chaudhary, 25-Jul-10 3:03
memberjonty chaudhary25-Jul-10 3:03 
Generalthanks a lot Pin
sortgnz, 13-Jun-10 22:26
membersortgnz13-Jun-10 22:26 
GeneralJust what I wanted Pin
Member 3400532, 8-Dec-09 5:37
memberMember 34005328-Dec-09 5:37 
GeneralInstallation : Error 1001 Pin
SohelElite, 6-Oct-09 22:09
memberSohelElite6-Oct-09 22:09 
GeneralSP1 doesn't work ?! ?! Pin
UBX, 7-Sep-09 3:53
memberUBX7-Sep-09 3:53 
QuestionHow to show the help file after installation Pin
Nicolas Stuardo, 18-May-09 5:25
memberNicolas Stuardo18-May-09 5:25 
Generalthanks Pin
himanshu1101, 5-May-09 19:29
memberhimanshu11015-May-09 19:29 
QuestionI got two erros when building Pin
alemos, 23-Feb-09 9:21
memberalemos23-Feb-09 9:21 
QuestionHow to modify MSI file? Pin
jp2code, 3-Feb-09 4:32
memberjp2code3-Feb-09 4:32 
Generalrun AFTER install? [modified] Pin
Ariadne, 14-Oct-08 5:55
memberAriadne14-Oct-08 5:55 
NewsThank you very much. Pin
zoidbeck, 7-Jul-08 3:04
memberzoidbeck7-Jul-08 3:04 
AnswerProper Way to Do This Pin
Robert Prouse, 23-Apr-08 5:34
memberRobert Prouse23-Apr-08 5:34 
GeneralRe: Proper Way to Do This Pin
rhennecke, 24-Apr-08 22:26
memberrhennecke24-Apr-08 22:26 
AnswerRe: Proper Way to Do This [modified] Pin
dmbrider, 28-Sep-08 11:39
memberdmbrider28-Sep-08 11:39 
Nice article… And nice responses’. There is always more than one way to skin a cat, or launch an application after it has been installed. One way, albeit a bit complicated, is to use the fact that MSIEXEC sets its main title to the ProductName of that being installed. In the Commit custom action of the deployment project for the application, set the CustomActionData Property to /ProductName="[ProductName]". Then in the installer class of the application, handle the Committed Event, as Bill has suggested, and retrieve the product name by adding the line:

String productName = Context.Parameters["ProductName"];

Next launch the application, as Bill has shown (well not quite. Bill tacks the exe name on to the end of Assembly.GetExecutingAssembly().Location, but this call actually returns the fully qualified path the executing assembly), and pass productName variable value to the Start method also:

Process.Start(Assembly.GetExecutingAssembly().Location, productName);

Now the complicated part… To the Main method of the application (not the only place to do this, but it is before the main window of the application is displayed, if it is a windows forms application), add a test for a command line parameter, that command line parameter being the ProductName discovered in the committed event handler. If the proper command line parameter has been passed, next get a list of all the processes running and loop through each looking at each MainWindowTitle for a match to the ProductName. Once a match has been found, hold on to the Process object that matched and just wait for the process to end before showing any windows in the application:

static void Main()
{
            Process installProc = null;
            string[] args = Environment.GetCommandLineArgs();
            if (args.Length == 2)
            {
                Process[] localAll = Process.GetProcesses();
                foreach (Process p in localAll)
                    if (p.MainWindowTitle == args[1])
                    {
                        installProc = p;
                        break;
                    }
                installProc.WaitForExit();
            }
            Application.Run(new Form1());
}
The Process that has a main window title equal to that passed to the application from the committed event handler is the running instance of the MSIEXEC that is installing the application. By waiting for the install to end, we avoid the problem of the application showing before the user has exited the install.

modified on Saturday, October 4, 2008 6:54 AM

GeneralRe: Proper Way to Do This Pin
vish111, 23-Oct-08 1:37
membervish11123-Oct-08 1:37 
GeneralRe: Proper Way to Do This Pin
RammohanT, 11-May-10 23:54
memberRammohanT11-May-10 23:54 
QuestionRe: Proper Way to Do This Pin
ROCNDAVE, 21-Jan-10 12:31
memberROCNDAVE21-Jan-10 12:31 
AnswerRe: Proper Way to Do This Pin
dmbrider, 24-Jan-10 6:08
memberdmbrider24-Jan-10 6:08 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150427.2 | Last Updated 11 Jul 2007
Article Copyright 2007 by Bill Daugherty II
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid