5

How can I dynamically change a connectionString in the app.config file?

I have an application written with windows forms, c# 3.0 and Linq to Sql. I need to change the connection string when i install the application. How i do this?

When the user installs the program it must show a form with an option to change the connection string if exists or add one if it doesn't.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
user95542
  • 333
  • 2
  • 8
  • 12

3 Answers3

6

If you are using a .NET deployment project, can achieve this by using Custom Actions.

Aaron Daniels
  • 9,563
  • 6
  • 45
  • 58
  • Do I need to write any vbs in custom actions? I have to create and store object of 3rd party class into settings at time of installation – Heena Jun 30 '17 at 09:51
5

Write a secondary config file with an appSettings block using the settings from the installer. In your main config file, use the file attribute in appSettings to reference the second config file, like so:

<appSettings file="User.config">

Settings in the secondary config will override any matching keys in the main config.

In your installer:

public override void Install(IDictionary stateSaver)
{
    base.Install(stateSaver);

    string server = Context.Parameters["Server"];
    string port = Context.Parameters["Port"];
    string targetDir = Context.Parameters["TargetDir"];
    // Build your connection string from user-input parameters and add them to dictionary

    WriteAppConfig(targetDir, server, port);
}

private void WriteAppConfig(string targetDir, string server, string port)
{
    string configFilePath = Path.Combine(targetDir, "User.config");

    IDictionary<string, string> userConfiguration = new Dictionary<string, string>();

    userConfiguration["Server"] = server;
    userConfiguration["Port"] = port;

    ConfigGenerator.WriteExternalAppConfig(configFilePath, userConfiguration);
}

public class ConfigGenerator
{
    public static void WriteExternalAppConfig(string configFilePath, IDictionary<string, string> userConfiguration)
    {
        using (XmlTextWriter xw = new XmlTextWriter(configFilePath, Encoding.UTF8))
        {
            xw.Formatting = Formatting.Indented;
            xw.Indentation = 4;
            xw.WriteStartDocument();
            xw.WriteStartElement("appSettings");

            foreach (KeyValuePair<string, string> pair in userConfiguration)
            {
                xw.WriteStartElement("add");
                xw.WriteAttributeString("key", pair.Key);
                xw.WriteAttributeString("value", pair.Value);
                xw.WriteEndElement();
            }

            xw.WriteEndElement();
            xw.WriteEndDocument();
        }
    }
}
Chris Doggett
  • 19,959
  • 4
  • 61
  • 86
  • I think this is the answer !! – Marshal Apr 12 '11 at 15:39
  • Can you pls guide me what should be the target dir path. I have included the file User.Config directly in the project, can't we just write User.config without combining it with target path. I tried the above code ..but not working. However, it doesn't fire any error either. – Marshal Apr 13 '11 at 05:24
  • 1
    @Marshal: Sorry I didn't see this before now, but if I recall correctly, TargetDir is a parameter set by the installer by default, and it should be set to the installation directory. Try throwing in a few logging statements to see what Context.Parameters["TargetDir"] is when you try to write the config file. – Chris Doggett Oct 22 '11 at 03:13
0

Check out this question. It has what you need to change values in the app.config dynamically through code.

Community
  • 1
  • 1
Dillie-O
  • 29,277
  • 14
  • 101
  • 140
  • I was looking at an old version of the question. Using the link I provided, you can have a popup screen the first time the application is started to prompt the user to change things. – Dillie-O Apr 24 '09 at 15:26