Working with Multiple Application Configuration files

Posted: May 28, 2014 in MS.Net Technology
Tags: , ,

At its simplest, the app.config is an XML file with built-in configuration sections and the ability to add your own custom settings. Custom settings can be added by using the built-in configuration sections (such as connectionStrings) or adding your own custom configuration sections. By adding an application configuration file (app.config file) to a C# project, we can customize many things like loading the common language runtime  and loading assembly files and many other.

I would like to take this opportunity to explain you an interesting thing which I have encountered during this week in one of the project that I am currently associated with.

I was asked to add unit test cases project to one of our existing project solution. The expectation is that we will be adding multiple folders to this projects and under each project we will be adding number of classes containing test cases. For example

  1. UnitTests>Core>Common folder contains all unit test cases related to Common Library
  2. UnitTests>Core>DataLayer folder contains all  unit test cases related to DataLayer Library
  3. UnitTests>Core> Security folder contains all unit test cases related to Security API added on our of project 

Now the requirements is as such UnitTests project would contain one master application configuration file and each sub folder in the project contains its own application configuration file with the required custom settings to run the unit tests added in various classes under that folder.

Which means while the unit tests are in-progress each set of unit test cases should read the required settings from its underlying custom configuration file.

<?xml version="1.0"?>

<section name="PostgreSQLConnectionInfo" type="System.Configuration.AppSettingsSection" requirePermission="true"
restartOnExternalChanges="true" allowLocation="true" />

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
<add key="ImportXMLFilePath" value="C:\Work\trunk\src\DataBaseTemplate.xml" />

<add key="Server" value="localhost" />
<add key="Database" value="kborra" />
<add key="User" value="kborra" />
<add key="Password" value="kborra" />


 In order to do that I have followed the below approach:

Added customapp.config files to each sub folder in the project. Created required test cases in each class and added such multiple classes to each sub folder.


 This has served my requirement. In a nut shell you have to add the below lines of code in-order to load a custom configuration file.

 public static class DBLayerUnitTests
 public static Configuration Configuration;

 static DBLayerUnitTests()
 Configuration =
 ReadConfiguration(Path.Combine(AssemblyDirectory, @"Core\DBLayer\MyCustomConfiguration.config"));

 var section = (Configuration.GetSection("appSettings") as AppSettingsSection);
 string dbtemplateFilePath = section.Settings["ImportXMLFilePath"].Value;

 var postgreSqlConnectionInfoSection =
 (Configuration.GetSection("PostgreSQLConnectionInfo") as AppSettingsSection);



 public static Configuration ReadConfiguration(string configFilePath)
 var configMap = new ExeConfigurationFileMap
 ExeConfigFilename = configFilePath
 return ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);


 This has served my purpose. In a nut shell you have to add the below lines of code in-order to load a custom configuration file.

ExeConfigurationFileMap map = new ExeConfigurationFileMap { ExeConfigFilename = "EXECONFIG_PATH" };
 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s