Updating a config key value at run time

Introduction:

In general, updating a config key is not a good practice because the appSettings section is a place to store connection strings, server names, file paths, and other miscellaneous settings needed by an application to work. The keys inside appSettings are items that need to be configurable depending upon the environment, for instance, any database connection strings will change as you move your application from a testing and staging server into production.

And such changes will, most often than not, be done only once in the lifetime of an application and not at runtime.

However, at times you might get in a situation where you actually need to do it. So, for the sake of it, this blog will guide you in achieving our objective of updating a config key value at runtime.

 

Implementation:

I’ve created a class Utility below. The class has a method UpdateKeyValue and it takes two parameters of type string namely-

  • key: name of the config key which is to be updated
  • value: new value of the key

We can use this class in any application  There is a slight difference in the way it is implemented for web based applications and other applications such as a console application.

Type: Web Application

For a web application, we have a web config file wherein we keep all our configuration keys. So, we are going to use class WebConfigurationManager here. You can read more about it here.

The code will update the value in the Web.config file and so the next time (request) you access the key again, you'll get an updated value of the key.

public class Utility
{
    public bool UpdateKeyValue(string key, string value)
    {
        try
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
            AppSettingsSection appsettings = (AppSettingsSection)config.GetSection("appSettings");
            if (appsettings != null)
            {
                appsettings.Settings[key].Value = "value";
                config.Save();
            }
        }
        catch (Exception ex)
        {
            //Log error with excpetion- "Couldn't update value, exception {0}", ex.Message
            return false;
        }
        return true;
    }
}

 

Type: Console Application

For other applications such as a windows service or a console application, we keep all our configuration keys in App.config file. So, here we are going to make use of ConfigurationManager class. You can read more about it here.

The code will update the value in the your_executable.exe.config but it will not be overwritten in App.config file. As soon as you access the key after updating its value, the key will have the updated value.

public class Utility
{
    public bool UpdateKeyValue(string key, string value)
    {
        try
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
            AppSettingsSection appsettings = (AppSettingsSection)config.GetSection("appSettings");
            if (appsettings != null)
            {
                appsettings.Settings[key].Value = "value";
                config.Save();
            }
        }
        catch (Exception ex)
        {
            //Log error with excpetion- "Couldn't update value, exception {0}", ex.Message
            return false;
        }
        return true;
    }
}