C#: How to modify web.config file – Winforms

Use this method to modify/add keys/values to the web.config file from windows application.

private void ModifyWebConfig(string path, string key, string value)
    var config = ConfigurationManager.OpenMappedExeConfiguration(
        new ExeConfigurationFileMap() 
            ExeConfigFilename = path 
    var element = config.AppSettings.Settings[key];

    // Check if element exists in web.config. if exists, simply update the value
    // if not, add a new key and its value
    if (element == null)
        config.AppSettings.Settings.Add(key, value);
        element.Value = value;


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s