Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 437
Irwin Avatar asked Aug 27 '26 15:08

Irwin


1 Answers

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");
}
like image 152
David Basarab Avatar answered Aug 30 '26 11:08

David Basarab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!