26

How can I share/link App.config or Web.config between multiple projects in a visual studio solution ?

Yoann. B
  • 11,075
  • 19
  • 69
  • 111

4 Answers4

43

In the Add Existing Item dialog that you get from Visual Studio's Solution Explorer, you can add a link to another file on disk to the project. In order to do this, you will have to click on the down-arrow on the right side of the Add button and choose Add As Link.

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • OK, I added it as a link, now how do I access the various values of the linked app config from the second project? – nawfal Jun 07 '12 at 12:53
  • @nawfal A linked `App.config` file gets *copied to the output folder* the same way as if it was in the project directory. From there you can access its contents through the [Configuration](http://msdn.microsoft.com/en-us/library/system.configuration.configuration.aspx) class. – Enrico Campidoglio Jun 07 '12 at 17:54
  • 1
    @EnricoCampidoglio, but the difference is in normal scenario you can just call the settings value like ConfigurationManager.AppSettings[key] etc without any hardcoding. But here I will need to hard code the config file name and then use open map configuration right? The problem is hard coding. I dont like it. Is there another solution? – nawfal Jun 07 '12 at 18:04
  • 1
    Beware that this method will not copy the app.config file to the obj folder on compilation and bindingredirects will not work on compilation which may cause that some assemblies may be resolved from the GAC/Path with incorrect older versions. Just in case. – Marc Climent May 05 '15 at 09:42
14

Another approach from Microsoft:

Use the fileattribute of the <appSettings> element to specify an external file which will define the common <appSettings> elements.

The external file will have the same schema as that of an app.config file with the exception that the root node must be <appSettings> rather than <configuration>.

Creating a common configuration file:

  1. On the File menu, point to New, then click File.
  2. In the New File dialog box, do the following:

    a. In the Categories pane, choose General.
    b. In the Templates pane, choose XML File.
    c. Click on the Open button to create a new common configuration settings file.

  3. Add a new <appSettings> element.

  4. Add as many common configuration settings as required within the <appSettings> node using <add> elements the same way as you would with any normal app.config file.

  5. Save the configuration file.

Specifying the common configuration file in each project

  1. Open the app.config file of each client project.

  2. Navigate to the <appSettings> element within the <configuration> node. If an element does not exist, add a new one.

  3. Add a new attribute file to the <appSettings> element and specify the relative path of the common configuration file as its value.
    The client project will now be able to access the common configuration settings.

Example

The following example shows how to define the common configuration settings in an external file.

        <?xml version="1.0" encoding="utf-8" ?>
        <appSettings>
            <add key="commonSetting1" value="MyApplication1" />
            <add key="commonSetting2" value="MySetting" />
        </appSettings>

The following example shows how to specify the path to the common configuration file in a project’s app.config file.

        <?xml version="1.0" encoding="utf-8" ?>
        <appSettings file=”c:\commonSettings.config”>
            <add key="myAppSpecificSetting" value="Setting1" />
        </appSettings> 

As shown in the example, a client project’s app.config file can have additional settings specified in the <appSettings> element in addition to pointing to the common configuration file. If the same setting is specified multiple times, the last value specified is used. If the same setting is specified in both the common configuration file and the client project app.config file, the value specified in the common configuration file is used.

nawfal
  • 70,104
  • 56
  • 326
  • 368
5

First of all, remember that an App.config, or web.config, is visble to all code running in any project that is a used within the "process" established by the start-up project within a solution... i.e. if your solution has one console app and 5 class libraries, or one WinForms app and 4 class libraries, or one WIndows service and 3 class libraries, or one console app being used as startup project, and another console app being used as a class library, then you have no problem, the configuration app.config and all referenced files, are visible from all of the projects.

Secondly, if you will have two or more separate executable processes running in your solution, (like a windows service as a server, AND a winforms client), then if you want them to share specific config settings, you could put those settings in the machine.config file, although you should be careful in doing this.. and some enterprise server teams frown on it..

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
2

According to the Post of nawfal, use the configSource attribute, if you want to manipulate the appSettings with the ConfigurationManager:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings configSource=”c:\commonSettings.config”>
    <add key="myAppSpecificSetting" value="Setting1" />
</appSettings>
Goldi
  • 83
  • 1
  • 10
  • There are already a few good answers and a working solution as well. – L. Guthardt Mar 08 '18 at 14:13
  • Yes, but in my case I already had a solution as eg. Enrico C. described. But I had problems in changing appSettings during the run time of my program (it's a solution that has a GUI project and a windows service project, and these projects share the same appSettings). I just wanted to add an answer for the case of required run time changes in the settings. – Goldi Mar 09 '18 at 06:05