Irwin
Irwin

Reputation: 12829

Modify Web.config during/after Web Setup project

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.

Upvotes: 3

Views: 2964

Answers (1)

David Basarab
David Basarab

Reputation: 73351

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");
}

Upvotes: 1

Related Questions