I want to use a single app.config by 3 different projects.
How to access the configurations?
ConfigurationManager.AppSettings["config1"]
I want to use a single app.config by 3 different projects.
How to access the configurations?
ConfigurationManager.AppSettings["config1"]
Let's say you have this folder structure:
Create the App.config file in the Solution level folder. In each project in Solution Explorer, right click and select Add Existing File
. Locate the file, and select Add as link
from the drop down box next to the Add
button.
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.
One design option is to avoid accessing the app.config directly from your class library projects altogether, thus avoiding the extra external dependency.
Rather, only your executable project knows about the config file and it can explicitly pass the appropriate config information to the libraries when it creates objects from them or initializes them.
I have found the button, and opened the app.config as link, however that caused when build to again create separate config file for each project, and therefore, when deploying the 3 project, i will have 3 config files. What I wanted to do, is keeping a single file for all projects in a certain solution. Can I do that?
Yes - you can do it, but should you do it?
The basic assumption in a .NET app is that one app = one config file. Out of the box, and with an easy method, you cannot share config files between applications.
If you create your own custom config sections, you could "outsource" those to external files, which could be shared. Imagine you create your own custom config section called "MyConfiguration", then your app.config would look something like that:
<configuration>
<configSections>
<section name="MyConfiguration"
type="MyConfigurationSection, MyConfigurationAssembly" />
</configSections>
<MyConfiguration>
<nestedElement>
<dateTimeValue>10/16/2006</dateTimeValue>
<integerValue>1</integerValue>
</nestedElement>
</MyConfiguration>
</configuration>
You could have your "MyConfiguration" section in its own file, and reference it from your app's config:
<configuration>
<configSections>
<section name="MyConfiguration"
type="MyConfigurationSection, MyConfigurationAssembly" />
</configSections>
<MyConfiguration configSource="MyConfiguration.config" />
</configuration>
and your "MyConfiguration.config" would then contain:
<MyConfiguration>
<nestedElement>
<dateTimeValue>10/16/2006</dateTimeValue>
<integerValue>1</integerValue>
</nestedElement>
</MyConfiguration>
By doing this, you could "externalize" and thus share at least the bulk of your config settings - provided they're in your own custom config sections.
For more info and an excellent intro to .NET 2.0 and up configuration mysteries, see Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.
Highly recommended, well written and extremely helpful!
Marc
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;