Click here to Skip to main content
13,260,330 members (44,493 online)
Click here to Skip to main content
Add your own
alternative version


87 bookmarked
Posted 11 Jul 2007

Launching Your Application After Install using Visual Studio 2005

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


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.


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:
    ' system.configuration.configurationmanager.aspx
    ' Set 'RunInstaller' attribute to true.

    <RunInstaller(True)> _
    Public Class InstallerClass
        Inherits System.Configuration.Install.Installer
        Public Sub 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("Committing Event occurred.");
        End Sub

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

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

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

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


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:
  // system.configuration.configurationmanager.aspx
  // Set 'RunInstaller' attribute to 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("Committing Event occurred.");

    // Event handler for 'Committed' event.
    private void MyInstaller_Committed(object sender, InstallEventArgs e)
          Assembly.GetExecutingAssembly().Location) + "\\InstallSample.exe");
        // Do nothing... 

    // Override the 'Install' method.
    public override void Install(IDictionary savedState)

    // Override the 'Commit' method.
    public override void Commit(IDictionary savedState)

    // Override the 'Rollback' method.
    public override void Rollback(IDictionary 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.


  • 11th July, 2007: Initial post


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


About the Author

Bill Daugherty II
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Generalthanks Pin
himanshu11015-May-09 19:29
memberhimanshu11015-May-09 19:29 
QuestionI got two erros when building Pin
alemos23-Feb-09 9:21
memberalemos23-Feb-09 9:21 
QuestionHow to modify MSI file? Pin
jp2code3-Feb-09 4:32
memberjp2code3-Feb-09 4:32 
Generalrun AFTER install? [modified] Pin
Ariadne14-Oct-08 5:55
memberAriadne14-Oct-08 5:55 
There are two problems with this approach:

1st: The app runs BEFORE the installation is finished. (The commited event is fired before the installer displays its last window.)

2nd: The app runs in the installer enviroment with adminstrator rights and anministrator registry. All settings are not available, if the user runs the app the next time with his own registry settings. It is very complicate to change this inviroment.

My Solution:
1st problem:
Start InstallSample /i in the Installer class
Add in the beginning of InstallSample:
static void  Main(<code>string[]sArgs</code>) 
<code>if (sArgs.Length>0&&sArgs[0] == "/i")
  do {Application.DoEvents();} 
  while (Process.GetProcessesByName("msiexec").Length>1); //wait until msiexec exits
Application.Run(new frmMain());

2nd problem:
replace the
     Assembly.GetExecutingAssembly().Location) + "\\InstallSample.exe");

in MyInstaller_Committed with
startUserProcess(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\InstallSample.exe /i");

and add the sophisticated startUserProcess
private void startUserProcess(string sCmd)
        if (!EventLog.SourceExists("InstallSample"))
            EventLog.CreateEventSource("InstallSample", EventLog.GetEventLogs()[0].LogDisplayName);
        IntPtr TokenExplorer = new IntPtr(0);
        IntPtr DupedTokenExplorer = new IntPtr(0);
        Process pExplorer;
        WindowsIdentity wExplorer;
        try //find process 'explorer' in the process-table (and adopt it's user)
            pExplorer = Process.GetProcessesByName("explorer")[0];
            IntPtr hExplorer = OpenProcess(ProcessAccessFlags.All, false, (uint)pExplorer.Id);
            OpenProcessToken(hExplorer, TOKEN_ALL_ACCESS, out TokenExplorer);
            wExplorer = new WindowsIdentity(TokenExplorer);
        catch (Exception ex)
            EventLog.WriteEntry("InstallSample", "Unable to Locate process 'explorer': " + ex.Message + "\nterminating...", EventLogEntryType.Error, 1);
        EventLog.WriteEntry("InstallSample", "the user-process will start in this user-enviroment: " + wExplorer.Name, EventLogEntryType.Information, 1);
            sa = new SECURITY_ATTRIBUTES();
            sa.bInheritHandle = false;
            sa.Length = Marshal.SizeOf(sa);
            sa.lpSecurityDescriptor = (IntPtr)0;
            const uint GENERIC_ALL = 0x10000000;
            const int SecurityImpersonation = 2;
            const int TokenType = 1;
            if (!DuplicateTokenEx(TokenExplorer, GENERIC_ALL, ref sa, SecurityImpersonation, TokenType, ref DupedTokenExplorer))
                throw new Win32Exception(Marshal.GetLastWin32Error());
        catch (Exception ex)
            EventLog.WriteEntry("InstallSample", "Unable to duplicate token: " + ex.Message + "\nterminating...", EventLogEntryType.Error, 1);
        try  //start user cmd with identity of the explorer process
            STARTUPINFO si = new STARTUPINFO();
            si.cb = Marshal.SizeOf(si);
            si.lpDesktop = "";
            if (!CreateProcessAsUser(DupedTokenExplorer, null, sCmd, ref sa, ref sa, false, 0, (IntPtr)0, "c:\\", ref si, out pi))
                throw new Win32Exception(Marshal.GetLastWin32Error());
        catch (Exception ex)
            EventLog.WriteEntry("InstallSample", "CreateProcessAsUser (" + sCmd + ") failed with " + ex.Message, EventLogEntryType.Error, 1);
            try //exit handling: return in every case!
            catch { }
    catch { }   //alle möglichen Fehler abfangen

Thanks to Jaisvar and his code

btw. 'taken'-line your installerClass should be changed to:


modified on Sunday, December 7, 2008 3:20 PM

NewsThank you very much. Pin
zoidbeck7-Jul-08 3:04
memberzoidbeck7-Jul-08 3:04 
AnswerProper Way to Do This Pin
Robert Prouse23-Apr-08 5:34
memberRobert Prouse23-Apr-08 5:34 
GeneralRe: Proper Way to Do This Pin
rhennecke24-Apr-08 22:26
memberrhennecke24-Apr-08 22:26 
AnswerRe: Proper Way to Do This [modified] Pin
dmbrider28-Sep-08 11:39
memberdmbrider28-Sep-08 11:39 
GeneralRe: Proper Way to Do This Pin
vish11123-Oct-08 1:37
membervish11123-Oct-08 1:37 
GeneralRe: Proper Way to Do This Pin
RammohanT11-May-10 23:54
memberRammohanT11-May-10 23:54 

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.

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