How to Share App.config?
Solution 1:
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.
Solution 2:
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:
- On the File menu, point to New, then click File.
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.Add a new
<appSettings>
element.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.Save the configuration file.
Specifying the common configuration file in each project
Open the app.config file of each client project.
Navigate to the
<appSettings>
element within the<configuration>
node. If an element does not exist, add a new one.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.
Solution 3:
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..