Click here to Skip to main content
Click here to Skip to main content

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

By , 18 Nov 2004
 

Introduction

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" ?>
<configuration>
 <appSettings file="settings.config">
 </appSettings>
</configuration>

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

<appSettings>
 <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" />
</appSettings>

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 = _
   System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")

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.

License

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
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
Questionmany websites, one code folder, config file in virtual directorymemberrhjiv24 Feb '09 - 14:50 
I have an application that shares the same code base for many instances of the application, but each instance needs its own config file full of appSettings. I want to put the config file in a virtual directory so that each site can have it's on config file, but it doesn't work.
 
My Web.config file contains this line:
 
My site has a virtual directory named "Config" that contains the file "client.config"; however, when I try to open the site, IIS cannot find the client.config file and read its settings.
 
Can you help?
 
Bob Jones - bob@thesocratesgroup.com

Generalrunning debug mode on web sitememberlittlecanary3 Jul '07 - 10:03 
I have built a utility with its own custom config file. When I use it in a console of winform application it works fine. When I use it in a web site, placing the utility and the custom config file in the bin directory, the web site that is built for debugging does not put the custom config file in the temp debug directory where the site is running. Where can I put this in the solution to have this be found by my utility.
 
littlecanary
GeneralReference to another configSectionsmemberM_A_MaDeRo11 Jan '07 - 7:40 
Anyone knows about how to do the same but to other config sections.
 
For example:

or

 
There are a lot of things that change in those files, but a lot more than stays just the same btw development and production, so something like that would be really helpful. Anyway appSettings is just an easy way of configuration and really limited (key/value only), but for serius configurations you need other configSections.
GeneralUse extension "exclude" in ASP.NET 2.0memberMichael Freidgeim8 Feb '06 - 17:22 
In Asp.NET 2.0 it is a good idea to give external file extension "exclude"
that will prevent it from publishing with Visual Studio "Publish Web Site" method.
 
Michael Freidgeim.
Blog: http://geekswithblogs.net/mnf/
GeneralPreferencememberSara Ramli27 Dec '05 - 19:44 
In case, there is a appsetting with same name in both the local config file as well as in the external config file [referred to with the "file" attribute], the value in the external config file takes preference. This is what the MSDN documentation states and I myself have verified it. Guess this needs correction in the article. Please correct me if I am wrong.
GeneralUse this to switch 'environments'memberAshley van Gerven19 Nov '04 - 12:05 
One particularly useful application i just thought of - switch between dev / live environment settings;
 
<appSettings file="settings_dev.config">
 
<appSettings file="settings_live.config">
 

BTW, one issue i have with storing settings in the web.config; when you modify this file it restarts the whole application (which I believe resets all active sessions too). So this doesn't seem to be the best way to store trivial settings (eg user interface messages..)
 

 
to be honest, I'd rather be dragged backwards thru a cactus garden than code VB!
QuestionRe: Use this to switch 'environments' [modified]memberchallengerking11 Jun '06 - 17:23 
Dear Ashley van Gerven:
I'm a programmer of China, i have a question about the multiple configuration file.
in Visual Stdio 2003 i could set multiple external config file in the App.config, just like this:





but in Visual Stdio 2005 it seems wrong, there may be include one external file only, liike this:

 

 

so i want to know if this is the new feature of 2.0, can you help me? thank you!

Best wishes.Smile | :)

 
love life.
 
-- modified at 23:49 Monday 12th June, 2006
GeneralShort, sweet and useful.memberDr Herbie18 Nov '04 - 22:47 
I never knew this. It's very useful. It didn't take long to read and understand.
 
Got my 5.
 
Dr Herbie
Remember, half the people out there have below average IQs.
GeneralAnother method for web applicationsmemberbkeskin18 Nov '04 - 20:21 
You put your configuration file into root directory of your web application. For each sub-application you create virtual directories and write their config files as well.
- Write the same parameter for all sub applications into root configuration file.
- If you want to override the value of a parameter, write it to sub-app config directory.
 
This cascading mechanism is so useful that you can even use it for sub-directories. I mean you don't have to use it by sub-applications.
 
Bulent Keskin
www.bulentkeskin.net
Software Developer

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

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