Configure App.config Application Settings During MSI Install

written by Rob Aquila on Wednesday, September 03 2008

I have been recently working on a setup project in Visual Studio 2008 to deploy a windows application and wanted the ability for the user to enter in values during the install that would then be saved in the app.config file.  After some playing around with and some research I figured out a way to do this. 

For this example I have a solution with two projects.  One project is the main application and the other is a setup project.  There are 3 variables that I will be capturing from the user and storing in the app.config file.  Here is what the app configuration settings look like in the app.config file.

<?xml version="1.0" encoding="utf-8" ?> 

<configuration

  <appSettings 

    <add key="Param1" value="" />  

    <add key="Param2" value="" />  

    <add key="Param3" value="" />  

  </appSettings 

</configuration> 

To update these 3 settings during the install process the first thing you would need to do if you haven't already is to create a setup project in Visual studio. 

image

 

Once the setup project is created you will need to add the main application's output by right clicking the setup project -> Add -> Project Output.  Then select primary output from the main project.

You will then need to create the dialog box that will be shown during the install process to collect the user's settings.  To do this right click the setup project -> View -> User Interface as shown below.

image

Once you see the list of dialogs as shown above, right click Start -> Add Dialog -> Text Boxes (A) to add the new dialog.  Once the dialog is created select it in the list and then navigate to that dialog's properties page.  The dialog that I have chosen has the ability to show 4 fields.  Since I only need to collect 3 parameters, I'm going to hide the 4th field.  As you can see below I set each field's label and set Edit4Visible to false.

image

The result is a dialog box that looks like this during the install process.

image

Now that you have a screen that will collect the variables from the user you will need a way to capture the values and save them into the app.config file.  This is where creating an installer class comes into play.  The installer gives you the hook you need to implement your custom code during the install process.  Add an installer class to the main application by right clicking the project -> Add -> New Item, choose Installer class.

image

 

After you create a new installer class in the main application, override the Install method.  Below is the code I created for this sample.  As you can see I collect all the user entered values from the context object.  I will explain how these values get passed into the context object in the next section.  I also capture the targetdir so that I can locate the app.config file.  Finally, I open the config file and update the application settings to the new values and save.  If you need to debug and step through your installer code you have to call Debugger.Break, since normal break points do not work while running an install.

[RunInstaller(true)]

public partial class MyInstaller : Installer 

{

      public MyInstaller()

      {

            InitializeComponent(); 

      }

      public override void Install(System.Collections.IDictionary stateSaver)

      {

            base.Install(stateSaver);

            string targetDirectory = Context.Parameters["targetdir"];

            string param1 = Context.Parameters["Param1"];

            string param2 = Context.Parameters["Param2"];

            string param3 = Context.Parameters["Param3"];  

            //System.Diagnostics.Debugger.Break();

            string exePath = string.Format("{0}MyWindowsFormsApplication.exe", targetDirectory);

            Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);

            config.AppSettings.Settings["Param1"].Value = param1;

            config.AppSettings.Settings["Param2"].Value = param2;

            config.AppSettings.Settings["Param3"].Value = param3;

            config.Save();

      }

} 

The final piece is to add a custom action so that the above installer code gets called.  To do this right click the setup project -> View -> Custom Actions.  You should then see a folder called install as shown below.  Right click that folder and select add custom action.  You should then see a dialog box, navigate to the Application Folder and select primary output from the main application.  Once that is completed navigate to the properties screen for the primary output that was added.  It should look something like the image below.

image

 

The final step is to add the CustomActionData values.  This string determines what gets passed into the context object that we used above to collect the user values that were entered.  As you can see, I'm storing each text box value into a param value.  I also pass in the targetdir.  Once this is complete, rebuild the solution and you now have an installer that can update the app.config file with the desired user values.  This is also useful when you want to have one version of the app.config file in your project that you use locally to debug with, and a deployed version with defaulted values set in the installer.

Similar Posts

  1. Team Foundation Server Build Notification Screen
  2. Using VSDBCMD and MSBUILD to Build and Deploy DBPro Projects
  3. Building .Net 1.1 Code In Team Foundation Server 2008

Comments are closed

Options:

Size

Colors