ala
ala

Reputation: 7290

single app.config multi-project c#

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

How to access the configurations?

ConfigurationManager.AppSettings["config1"]

Upvotes: 54

Views: 70976

Answers (7)

marc_s
marc_s

Reputation: 754230

Here's the "Add existing item" dialog in VS 2008:

Add existing item dialog

Click on the little dropdown indicator on the "Add" button and pick "Add as Link" from the context menu.

Marc

Upvotes: 9

Andy Braham
Andy Braham

Reputation: 10163

I understand this is a old question but there is a much easier way of achieving this. If you are using Visual Studio 2008 or higher there is a Project type called "Shared Project".

VS2015 C# Shared Project

A Shared Project can have pretty much anything that another types of Projects can contain. This is not only for C# but for all languages that VS2015 supports. When something is included in the Shared Project it is available to other projects after you add a reference to it (see below).

The major difference with Classes in a Shared Project VS a Shared Library is when you compile the program everything that is in the Shared Project will be Compiled directly into your project not as a separate file (.dll, .exe). Think of it like everything that is in the Shared Project is inserted into the other projects. Here is a small tutorial on setting this up and using it:

Visual Studio 2015 - Shared Project Tutorial:

Create the New Shared Project by selecting File->New->Project or Right Click on the Solution in the Solution Explorer and select Add->New Project. When the Dialog shows up select "Shared Project", give the Project a name TestShared in this example.

Add Shared Project

After the New Project is added you can add anything you need to be available to other projects. In this case we will add the app.config. Right Click on the Shared Project and select Add->New Item. Select Visual C#->Data->XML File naming it obviously to app.config.

app.config

Finally add a reference to the Shared Project by Right Clicking the Project that you need to share the project with and select Add->Reference. In the Reference Manager Dialog select your Shared Project, it will be listed under the "Shared Projects" item on the Left.

Add Reference

Now everything that is in the Shared Project is available in the other project, there is no need to do any using imports or anything like that. It simply works. This pattern is quite handy when you are developing a program and need to have several different GUI's (Windows, iOS, Android etc). For example you could have one shared project for the "Core" functionality and then have a separate GUI Project for each of the different Operating Systems you want to support in your program.

I realize that this is a older question but since this showed up in Google I thought I would answer this so when others are looking for the same thing they know about this very powerful VS feature.

Upvotes: 7

Jon Grant
Jon Grant

Reputation: 11530

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.

Upvotes: 68

Kapil dev
Kapil dev

Reputation: 233

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config = ConfigurationManager.OpenExeConfiguration(Path.Combine(@"D:\", "config.exe"));
foreach (string key in config.AppSettings.Settings.AllKeys)
{
   string value = config.AppSettings.Settings[key].Value;
   ConfigurationManager.AppSettings.Set(key, value);
}

Upvotes: 1

marc_s
marc_s

Reputation: 754230

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

Upvotes: 8

ala
ala

Reputation: 7290

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;

Upvotes: 21

Greg D
Greg D

Reputation: 44066

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.

Upvotes: 7

Related Questions