How to share custom application configuration settings across projects in .NET

Author:

Rakesh Rajan MVP

COMMUNITY SOLUTIONS CONTENT DISCLAIMER

MICROSOFT CORPORATION AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, OR ACCURACY OF THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN. ALL SUCH INFORMATION AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, WORKMANLIKE EFFORT, TITLE AND NON-INFRINGEMENT. YOU SPECIFICALLY AGREE THAT IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OF OR INABILITY TO USE THE INFORMATION AND RELATED GRAPHICS CONTAINED HEREIN, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES.

SUMMARY

This article describes how to use the "file" attribute of the "<appSettings>" element in app.config to share custom application configuration settings across projects in .NET.

You have multiple .NET projects and want to share common application configuration settings specified in the <appSettings> element

You are developing multiple .NET projects. These projects have common custom configuration settings specified in the <appSettings> element. You want to share these settings so that they need not be repeatedly specified in the app.config files of all projects.

Q

How can I share settings specified in <appSettings> element across multiple projects in .NET?

A

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 file1. 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 project1. Open the app.config file of each client project.2. Navigate to the <appSettings> element within the <configuration> node. If an <appSettings> 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 it’s value.

The client project will now be able to access the common configuration settings.

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.