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:
Continue reading Modify App Configuration while running application , C#.NET

Jaagrugta Failao – By Sunil Singhal