Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

single app.config multi-project c#

Tags:

c#

config

I want to use a single app.config by 3 different projects.

How to access the configurations?

ConfigurationManager.AppSettings["config1"] 
like image 378
ala Avatar asked Sep 01 '09 11:09

ala


2 Answers

Let's say you have this folder structure:

  • Solution
    • Project1
    • Project2
    • Project3

Do this:

  1. Create the App.config file in the Solution level folder. You won't find an option to add an App.config file from the templates, so just create a new empty text file with the name App.config, and paste in the contents of a regular App.config file.
  2. For each project in Solution Explorer:

    1. Right click and select Add > Existing Item
    2. Locate the file
    3. Select Add as link from the drop down box next to the Add button.

      Add as link

Edited to add:

You correctly state that the above method only shared the file up to build-time. To use a shared file at run-time, see the answers to this question.

like image 159
Jon Grant Avatar answered Oct 13 '22 20:10

Jon Grant


The common config file

<?xml version="1.0" encoding="utf-8" ?> <configuration>     <configSections>         <section              name="appSettings"              type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"              />     </configSections>     <appSettings>         <add key="key1" value="value1"/>     </appSettings> </configuration> 

To access mapped config file

ConfigurationFileMap fileMap = new ConfigurationFileMap(file); //Path to your config file Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap); string value = configuration.AppSettings.Settings["key1"].Value; 
like image 33
ala Avatar answered Oct 13 '22 22:10

ala