Sharing single Configuration in Multiple projects

Recently I have came across to a problem where I need to share the same configuration file in multiple projects under the same solution. Well, I think it is very common scenario where a single Application Configuration File (app.config) or Web Configuration File (web.config) needed to be used to multiple projects on the same solution. Hence I thought of putting it as tip for you.

Lets see how to do this in steps.

Say I have two projects added in the same solution, and I need to use the same configuration file for both the projects.

Step 1: Place the App.Config file in a folder parent to both(or all) the projects that needs to use the same Config file. I have put it in a new folder called AppConfig.
Step 2: Right click on each project and select the same App.config file but add it as Link. You can see how to get the link from the image :

Once you add the Configuration file in both the projects as link this configuration will work from either projects.

To use this configuration file you need to use ConfigurationManager. Lets see how to use it.


But this configuration will not work after you build the project and deploy somewhere. The deployed application needs a configuration file in same name as the exe to load it automatically. Hence you need to programmatically load the configuration file to deal with such a situation.

Lets say we have deployed the configuration file in the parent directory of the applications. Now to add the same file we use :

ExeConfigurationFileMap efm = new ExeConfigurationFileMap { ExeConfigFilename = "AppConfig/app.config" };
Configuration configuration = ConfigurationManager.OpenMappedExeConfiguration(efm, ConfigurationUserLevel.None);
if (configuration.HasFile)
AppSettingsSection appSettings = configuration.AppSettings;
KeyValueConfigurationElement  element = appSettings.Settings["myconfig"];

Here we use the Api OpenExeConfiguration to read the configuration file from runtime and parse the settings. You can also use GetSection from Configuration to get a section by name and load your custom section.

For Web projects, Virtual directory maps all the sub folders to its parent automatically. But if you want to use more than one virtual directory to load the same web.config, I mean more than one Web application, then you need to use the same approach as shown.

I hope this would help
Thank you for reading.

Abhishek Sur

Abhishek Sur is a Microsoft MVP since year 2011. He is an architect in the .NET platform. He has profound theoretical insight and years of hands on experience in different .NET products and languages. He leads the Microsoft User Group in Kolkata named KolkataGeeks, and regularly organizes events and seminars in various places for spreading .NET awareness. He is associated with the Microsoft Insider list on WPF and C#, and is in constant touch with product group teams. He blogs at His Book : Visual Studio 2012 and .NET 4.5 Expert Development Cookbook. Follow Abhishek at Twitter : @abhi2434