tags:

views:

1579

answers:

5

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

How to access the configurations?

ConfigurationManager.AppSettings["config1"]
+8  A: 

Let's say you have this folder structure:

  • Solution
    • Project1
    • Project2
    • Project3

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.

Jon Grant
I wonder if that was what the question was about...
Mark Seemann
thank you, I never thought i could add common items to the solution that will be shared among all projects. but i'm using VS2008, and could not find "Open As" link. plus the app.config file is not propagated to the projects.
ala
On the "Add existing item" dialog, in the lower left corner, there's a "Add" button with a tiny dropdown indicator to the right - click on that indicator and you'll get a context menu with "Add" and "Add as Link" - use "Add as link"
marc_s
Thanks marc_s, 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?
ala
Sorry I've corrected my answer to say "Add" instead of "Open" - didn't have VS right in front of me at the time :) You are right though, this only solves the shared config for build-time... See my next answer for this.
Jon Grant
+4  A: 

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.

Greg D
I agree with the design concern, but I have to build 3 separate console applications that use the same config file. Do you know if I can setup a common app.config file, and how to access appSettings?
ala
+3  A: 
marc_s
+2  A: 

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

marc_s
This solution will not work, unless all of the applications are deployed to the same directory. The configSource attribute cannot contain an absolute path nor a path to a parent directory.
Jon Grant
@Jon: ah, true, I keep forgetting that "only this directory or below" rule in .NET deployments..... you're right.
marc_s
+2  A: 

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;
ala