Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Share App.config?

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

like image 344
Yoann. B Avatar asked Jan 08 '09 22:01

Yoann. B


People also ask

How do I share a config file between projects?

Step 1: Place the App. Config file in a folder parent to both(or all) the projects that needs to use the same Config file. I have put it in a new folder called AppConfig. Step 2: Right click on each project and select the same App.

Where is App config file located?

The application configuration file usually lives in the same directory as your application. For web applications, it is named Web. config. For non-web applications, it starts life with the name of App.

Can we have multiple App config files?

You cannot use multiple configuration files (i.e. one per library project) without coding.

What is App config file?

An application configuration file is an XML file used to control assembly binding. It can redirect an application from using one version of a side-by-side assembly to another version of the same assembly. This is called per-application configuration.


4 Answers

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.

like image 127
Enrico Campidoglio Avatar answered Sep 29 '22 09:09

Enrico Campidoglio


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.

like image 25
nawfal Avatar answered Sep 29 '22 11:09

nawfal


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..

like image 20
Charles Bretana Avatar answered Sep 29 '22 10:09

Charles Bretana


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>
like image 43
Goldi Avatar answered Sep 29 '22 10:09

Goldi