views:

560

answers:

2

How do I modify the web.config of a web site during the setup's execution? I would like to have the user construct a connection string and then store that in the web config.

+1  A: 

This link (How To Modify Web.Config At Run Time) gives some insight.

Jeremy Cron
I don't know how to access to the web site/application folder from the setup project I am in.
Irwin
A: 

I have found this to work. But this at run time not set up.

public void ChangeAppSettings(string applicationSettingsName, string newValue)
{
    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    KeyValueConfigurationElement element = config.AppSettings.Settings[applicationSettingsName];

    if (element == null)
    {
        config.AppSettings.Settings.Add(applicationSettingsName, newValue);
    }
    else
    {
        element.Value = newValue;
    }

    config.Save(ConfigurationSaveMode.Modified, true);

    ConfigurationManager.RefreshSection("appSettings");
}
David Basarab