Executing Multiple MSI’s Dynamically
page 1 of 1
Published: 13 Feb 2006
Unedited - Community Contributed
Abstract
In this article, we will learn how the end users, through a splash screen, can select all the applications or a few applications for installation at once. This article also demonstrates how to execute the MSI files through code.
by Vishal Kumar Patil
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 21080/ 53

 

Overview

In most of the projects, multiple applications need to be delivered at once to the client. Instead of creating a setup file (or MSI File) for each application and delivering all the setup files to the client, the better solution is to provide a Main Setup file that contains setup files of each application. The Main Setup file will also have the splash screen, which executes the Setup files in the Main Setup file. When executed, the Main Setup file pops up a splash screen, enabling end-users to select all the applications or a few applications for installation. This simplifies the process of deployment.

This article demonstrates how to implement the same thing through simple custom code.

Software Requirements

Visual Studio .NET 2003

Creating the Setup for Each Application

The steps given below describe the process to create the setup for each application.

1.    Add a Windows Application project with the name Application1. To this, add a form with a Label. In the Label, set the text as Welcome to Application1. Now add a Setup project with the name Application1Setup.

2.    In the File System Editor (SetUp), add the above Windows project as the Primary Output project in the Application Folder.

3.    Compile the solution.

4.    Similarly, create two more applications as Application2 and Application3, and for each of these applications, create Setups as Application2Setup and Application3Setup.

Designing the Splash Screen

Create a Windows Application project with the name SplashScreen. To this, add a form which should look as follows.

Figure 1

 2. Add the following code by double-clicking the Button control labeled “Install.”

Listing 1

private void btnInstall_Click(object sender, System.EventArgs e)
{
  StreamWriter sw = null;
  try
  {
    if (ChkApplication1.Checked == false &&
      chkApplication2.Checked == false &&
      chkApplication3.Checked == false )
    {
      MessageBox.Show("Please Select the Application, You 
        would like to Install");
    }
    else
    {
      Assembly objAssembly = Assembly.GetAssembly(this.GetType());
      string strAppPath= objAssembly.CodeBase;
      strAppPath = strAppPath.Replace("SplashScreen.dll","");
      Uri objUri = new Uri(strAppPath); 
      strAppPath = @objUri.AbsolutePath; 
 
      string strBatchFilePath = strAppPath + @"Setup.bat";
      
      sw = File.CreateText(strBatchFilePath);
 
      string ReadString = "@echo off";
      sw.WriteLine(ReadString);
        
      if (ChkApplication1.Checked == true)
      {
        string strAppltn1Path = strAppPath + "Application1Setup.msi";
        strAppltn1Path = strAppltn1Path.Replace("/","\\");
        strAppltn1Path = @"msiexec /i " + strAppltn1Path;
          
        sw.WriteLine(strAppltn1Path);
      }
 
      if (chkApplication2.Checked == true)
      {
        string strAppltn2Path = strAppPath + "Application2Setup.msi";
            
        strAppltn2Path = strAppltn2Path.Replace("/","\\");
        strAppltn2Path = @"msiexec /i " + strAppltn2Path;
          
        sw.WriteLine(strAppltn2Path);
      }
 
      if (chkApplication3.Checked == true)
      {
        string strAppltn3Path = strAppPath + "Application3Setup.msi";
        strAppltn3Path = strAppltn3Path.Replace("/","\\");
        strAppltn3Path = @"msiexec /i " + strAppltn3Path;
          
        sw.WriteLine(strAppltn3Path);
      }
      sw.Flush();
 
      sw.Close();
 
      System.Diagnostics.Process.Start(strBatchFilePath);
 
      Close();
    }
  }
  catch(Exception ee)
  {
    MessageBox.Show(ee.Message);
  }
  finally
  {
    sw = null;
  }
}

In the above listing, first the current assembly path is fetched, and the batch file named Setup.bat is created in the same assembly path. Then, it checks which application is selected by the end-users for installation. The selected applications' MSI file path is fetched, concatenated with the command “msiexec /i,” and written into the batch file. The command “msiexec /i” executes the MSI file.

Once all the selected applications are written into the batch file, this batch file is executed by the System.Diagnostics.Process.Start namespace method, which executes the MSI files contained in this batch file one by one.

Also, add the code shown below in the Checkbox CheckedChanged event.

Listing 2

private void chkSelectAll_CheckedChanged(object sender, System.EventArgs e)
{
if (chkSelectAll.Checked == true)
{
    ChkApplication1.Checked = true;
    chkApplication2.Checked = true;
    chkApplication3.Checked = true;
  }
  else
  {
    ChkApplication1.Checked = false;
    chkApplication2.Checked = false;
    chkApplication3.Checked = false;
  }
 
}

In the above listing, when the chkSelectAll checkbox is selected, all the applications (Application1, Application2, and Application3) will be selected for installation. With chkSelectAll unselected, all the applications are unselected. Finally, compile the project.

Creating the Main Setup Consisting of Individual Setups and Splash Screen

Now add another Setup project as MainSetup.

To this, add the above SplashScreen project as the Primary Output project in the Application Folder.

And now add Application1Setup, Application2Setup, and Application3Setup to the Application folder (Setup) as shown below.

Figure 2

In the Custom Action Editor (Set Up), add SplashScreen as the Primary Output project to the Install folder as shown below.

Figure 3

Now compile the project.

Executing the Main Setup

Execute the Main Setup file, on which the splash screen is displayed (see Figure 4). In the splash screen, select the Applications you want to deploy (suppose Application1 and Application2 are selected), and click the Install button. This executes first the Application1Setup; on completing the Application1Setup execution, it executes the Application2Setup.

Figure 4

                 

Now go to c:\DynamicMSI -> and find Application1 and Application2 as shown below.

Figure 5

         

Click Application1.exe, on which a screen is displayed with the text "Welcome to Application1." Similarly, perform the above step for Application2.

On selecting the Select All option in the splash screen, all the applications will be selected for    installation, and they will be executed one by one when the Install button is clicked.

Conclusion

In this article, I have demonstrated how to provide the splash screen, which enables end users to select some of the applications or all the applications at once for installation using Visual Studio .NET 2003. You have also seen how to execute an MSI file dynamically.

Downloads

[Download Sample]



User Comments

Title: Executing Multiple MSI’s Dynamically   
Name: Anjana
Date: 2012-02-01 4:57:55 AM
Comment:
hello,
this is very gud article,but images are not visible which will give better understanding.And can we get to know the state of MSI..ie its installation got completed or under progress and all...if that is provided it'll be more helpfull
Thank you
Anjana
Title: Executing Multiple MSI’s Dynamically   
Name: Madhav Shiraguppi
Date: 2009-12-10 2:31:11 AM
Comment:
Hello, this is an excellent article. i followed this and i'm able to install the msi's dynamically. However i tried to do the same for uninstall also but it fails with error code 2869 and the uninstall process rolls back. Any resolution for this would be greatly appreciated.
Thanks in advance
-- Madhav
Title: Executing Multiple MSI’s Dynamically   
Name: John Ritch
Date: 2006-07-09 11:12:40 AM
Comment:
Very nice and just what I was looking for. Easy to understand and follow. Thanks!






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-10-18 7:58:08 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search