Question

My applications have a handful of config files, each one particular to the machine it's being deployed to (dev, production, etc.) I'm wanting to add them to the setup project, use radio buttons to select one and then have a script (or maybe custom action?) rename the selected files to connectionStrings.config, settings.config, etc.

Is this feasible/possible with a setup project?

To give you an idea, my configs might look like this:

DEV connectionStrings.config
PROD connectionStrings.config

After the user chooses DEV or PROD in the installer radiobutton UI, I would like the chosen config to be renamed to

connectionStrings.config

Considering it's a VS setup project, I have a feeling I'm asking for way too much and that I will get an interesting response as most setup project questions do :)

Was it helpful?

Solution

I've come to the conclusion that this is impossible due to VS severely lacking on setup project configuration options. Instead I added a radio button control during the setup process and assigned the choices a variable name. In the file system I added all of my config files and then set conditions to each one. They referenced values to my radio button choices in order to copy during deployment.

OTHER TIPS

I created a setup project to set connection strings and i used the following which works perfectly for me.

Create a installer.cs file for the setup. using System;

using System.Data.SqlClient;
using System.Xml;
using System.Configuration;
using System.Reflection;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Configuration.Install;


namespace YOURNAMESPACE
{

    [RunInstaller(true)]
    public partial class installer : System.Configuration.Install.Installer
    {

        public installer()
        {
            InitializeComponent();
        }
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);

        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);

            try
            {

                string DatabaseString1 = "FULL NAME OF CONNECTION STRING";
                ConnectionConfigure(DatabaseString1);


            }
            catch (Exception e)
            {
                base.Rollback(savedState);
            }
        }

        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }

        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);
        }


        private void ConnectionConfigure(string DatabaseString)
        {
            string dataSource = "";
            dataSource = "Provider="+ Context.Parameters["InitialCatalog"]+ ";" + "Data Source=" + Context.Parameters["DataSource"];
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();

            string configFile = string.Concat(Assembly.GetExecutingAssembly().Location, ".config");
            map.ExeConfigFilename = configFile;
            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.
            OpenMappedExeConfiguration(map, System.Configuration.ConfigurationUserLevel.None);
            string connectionsection = config.ConnectionStrings.ConnectionStrings
            [DatabaseString].ConnectionString;


            ConnectionStringSettings connectionstring = null;
            if (connectionsection != null)
            {
                config.ConnectionStrings.ConnectionStrings.Remove(DatabaseString);

            }

            connectionstring = new ConnectionStringSettings(DatabaseString, dataSource);
            config.ConnectionStrings.ConnectionStrings.Add(connectionstring);

            config.Save(ConfigurationSaveMode.Modified, true);
            ConfigurationManager.RefreshSection("connectionStrings");

        }


    }
}

Add project output to your setup project then begin to setup this setup project.

Right click setup project

Add text boxes and create the UI

Set custom actions

Create project output actions

Custom action properties

That is how i setup mine (I have attached screenshots to help explain my process but in short. Create setup project and installer.cs file. Add project output to setup project, add a UI so that the user can input a connection string and or provider for connection string, add custom actions so that the inputs can be read by the installer.cs file and then congratulations it should change the connection string. Hope this helps.

I've done this many times, and basically I just install both files with different names. The application can ask the user which file to use, and change it anytime they want because many users don't know enough about the application to make this choice at install time.

You get interesting answers to setup questions like this because many people want to configure the application during the installation. Why not just let the setup install the files and have the app do its own configuration?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top