Modify App Configuration while running application , C#.NET

Each application has it’s own configuration file, be it a windows based application or web based.
This application configuration file defines information which can be used by application to make decisions, to load some other information or may contain the custom configuration which can be empowered to do anything.
There can also be scenarios where an application may want to change\modify the existing setting in the application configuration file and those changes should not only take effect immediately but should also be persisted.

For this,
APIs are available which can load and modify the configuration despite reading.

Make use of Configuration class in System.Configuration namespace.
– Add the reference to System.Configuration to your project
– Create a function which does the following:

  • Make a call to OpenExeConfiguration() method
  • Get a reference to the section which is used by module
  • Make a call to Add\Remove or access the Value property to change the value
  • Make a call to Save() method
  • And, finally refresh the Configuration Section by calling RefreshSection() so that the next read from Config file happens by reading it from disk.

– Call the above function with proper values before invoking a method to be unit tested
– Do the clean up if required before executing another test case

Following is the code snippet which adds\changes the key-vale pair in the appSettings section of the config file:

private const string ProviderKey = "NameOfProviderGoesHere";
private static void AddDisableFlagToConfig(string value)
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (ConfigurationManager.AppSettings[ProviderKey] == null)
config.AppSettings.Settings.Add(ProviderKey, value);
config.AppSettings.Settings[ProviderKey].Value = value;


  • Saving user preferences. Consider GUI applications (Windows based\Desktop apps) where user can specify it’s preferences. When a user starts the application for the first time, application loads the default configuration and then asks that user for it’s preferences. Those preferences should then saved so that whenever that user starts that application, app should load his saved configuration and not default.
  • Developers write modules providing functionality mapping to requirements. It is necessary to unit test a module, to determine
    – the correctness of module functionality
    – code coverage
    before handing it over to a test team.

    A module can do a decision making on the basis of some configuration value, for e.g., a key-value pair in appSettings section.
    Consider the following scenario:

    private const string ProviderKey = "NameOfProviderGoesHere";

    private void LoadConfiguration()
    string selectedMessageBus = ConfigurationManager.AppSettings[ProviderKey];
    if(string.IsNullOrEmpty(selectedMessageBus) || selectedMessageBus.Equals("MSMQ"))
    // do operations on MSMQ
    else id(selectedMessageBus.Equals("MQ"))
    // do operations on IBM Websphere MQ

    You have a component which can talk to both MSMQ or MQ depending upon the configuration which can be set as per environment.
    You want to unit test the above function as well as guarantee that you have not written any dead code.
    You wrote 3 unit tests, for each scenario: default, MSMQ, MQ and in your test project, you added an app configuration file (no app configuration file mocking here).[I’m not a big fan of mocking everything, specially appConfig file. It’s already test proven]

    So far it’s good. I have done my homework.

    But, what will happen when you’ll run your unit tests? Do you expect that you will get full coverage results?
    Answer is NO.
    All code branches will not be executed since app config has or can define only value for a key.

    Now, the question arises.
    Code is perfect and branch is definitely needed. Unit Tests will be run as part of a code build and editing app config manually prior to running unit test is not an option.
    How will i achieve full coverage results through unit tests?

    Can i edit the app config programmatically? before running each test case?
    Hmmm, yes. Makes sense but how? Any in-built support? Yes.
    Use the above method before executing a test.


Leave a Reply

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

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

Facebook photo

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

Connecting to %s