Click here to Skip to main content
13,863,682 members
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 18 Nov 2004

Using the File attribute of the appSettings element of a .NET config file

, 18 Nov 2004
Rate this:
Please Sign up or sign in to vote.
Easily share configuation settings with the appSettings element File attribute.


If you need to share configuration settings among multiple .NET assemblies, the practice of maintaining separate config files for each assembly can quickly become tedious. For instance, if you have multiple executables within a directory that all need a 'ConnectionString' entry in their config file, the traditional method in .NET would be for each executable to have its own config file. This can become a burden in an enterprise environment when you need to change the connection string, as you would be forced to change each individual config file. Fortunately, there is a better approach. This better approach involves using the File attribute of the .NET config file's appSettings element. This attribute should contain the relative path to a custom config file, which all other applications can share. The description from MSDN on the appSettings File attribute follows:

Specifies a relative path to an external file containing custom application configuration settings. The specified file contains the same kind of settings that are specified in the <add>, <remove>, and <clear> elements and uses the same key/value pair format as those elements. The path specified is relative to the main configuration file. For a Windows Forms application, this would be the binary folder (such as /bin/debug), not the location of the application configuration file. For Web Forms applications, the path is relative to the application root, where the web.config file is located.

Note that the runtime ignores the attribute if the specified file can not be found.

Essentially, each executable's config file will contain an entry such as:

<?xml version="1.0" encoding="utf-8" ?>
 <appSettings file="settings.config">

where "settings.config" is a custom config file which looks something like this:

 <add key="Setting1" value="This is Setting 1 from settings.config" />
 <add key="Setting2" value="This is Setting 2 from settings.config" />
 <add key="ConnectionString" value="ConnectString from settings.confg" />

When you run your application, you simply use the AppSettings property from System.Configuration.ConfigurationSettings to reference the configuration setting, such as:

Dim s As String = _

In this case, if there is a 'ConnectionString' key in the individual config file, that value will be used. Otherwise, the value will be retrieved from the shared "settings.config" file.

As indicated above, if you choose, each individual config file can also define the keys which are present in the shared config file. In that case, the individual config file settings will take precedence over the shared config file. That could be useful in a situation where you want to quickly test a new setting without disrupting the other applications which are using the shared config file.

Hopefully, this article has shed some light on the sometimes confusing world of .NET configuration files. I hope many of you find this method useful when dealing with multiple configuration files. Good luck.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Scott Bradley
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

Questionmany websites, one code folder, config file in virtual directory Pin
rhjiv24-Feb-09 15:50
grouprhjiv24-Feb-09 15:50 
Generalrunning debug mode on web site Pin
littlecanary3-Jul-07 11:03
memberlittlecanary3-Jul-07 11:03 
GeneralReference to another configSections Pin
M_A_MaDeRo11-Jan-07 8:40
memberM_A_MaDeRo11-Jan-07 8:40 
GeneralUse extension "exclude" in ASP.NET 2.0 Pin
Michael Freidgeim8-Feb-06 18:22
memberMichael Freidgeim8-Feb-06 18:22 
GeneralPreference Pin
Sara Ramli27-Dec-05 20:44
memberSara Ramli27-Dec-05 20:44 
GeneralUse this to switch 'environments' Pin
Ashley van Gerven19-Nov-04 13:05
memberAshley van Gerven19-Nov-04 13:05 
QuestionRe: Use this to switch 'environments' [modified] Pin
challengerking11-Jun-06 18:23
memberchallengerking11-Jun-06 18:23 
GeneralShort, sweet and useful. Pin
Dr Herbie18-Nov-04 23:47
memberDr Herbie18-Nov-04 23:47 
GeneralAnother method for web applications Pin
fizikci18-Nov-04 21:21
memberfizikci18-Nov-04 21:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190214.1 | Last Updated 18 Nov 2004
Article Copyright 2004 by Scott Bradley
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid