AppSettings can Reference an External Config File

I recently discovered that the app/web.config file can reference an external config file to get some or all of its appsettings.  You don't have to name it with the extension config, but that's best since its protected from anyone browsing to it, and since it has the support of the VS.NET editor.  You can have keys that are only defined in the external file, or you can override keys that were already defined in the app/web.config file.  One great use of this would be to store your database connection string, or other settings that may vary from development to testing to production.  I did notice that changes made to the external config file are not automatically picked up until the app restarts, but thats easy to force by simply editing the real app/web.config.  By the way, this does fully work in the original version 1.0 .NET framework, although it was apparently undocumented until 1.1.  Sorry if someone else wrote about this -- I can't remember where I first saw this tidbit recently -- I think it may have been in the last MSDN Magazine but I'm not sure.

Web.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings file="YourSettings.config">
    <add key="KeyToOverride" value="Original" />
    <add key="KeyToNotOverride" value="Standard" />
  </appSettings>
  <system.web>
    <!-- standard web settings go here -->
  </system.web>
</configuration>

YourSettings.config:
<appSettings>
  <add key="KeyToOverride" value="Overridden" />
  <add key="KeyToBeAdded" value="EntirelyNew" />
</appSettings>

Published Wednesday, April 09, 2003 3:51 PM by PaulWilson

Comments

# re: AppSettings can Reference an External Config File

Cool!

Wednesday, April 09, 2003 2:11 PM by Chris Frazier

# app/web.config file can reference an external config file : Fabrice's weblog

app/web.config file can reference an external config file : Fabrice's weblog

Wednesday, April 09, 2003 3:51 PM by TrackBack

# re: AppSettings can Reference an External Config File : Paul Wilson's .NET Blog

re: AppSettings can Reference an External Config File : Paul Wilson's .NET Blog

Wednesday, April 09, 2003 3:51 PM by TrackBack

# Great tip by Paul Wilson... : Loosely Coupled

Great tip by Paul Wilson... : Loosely Coupled

Wednesday, April 09, 2003 3:51 PM by TrackBack

# AppSettings can Reference an External Config File : Wes' Puzzling Blog

AppSettings can Reference an External Config File : Wes' Puzzling Blog

Wednesday, April 09, 2003 3:51 PM by TrackBack

# re: AppSettings can Reference an External Config File

That looks really great Paul.

I have a quick question, can you use this method to define multiple additional config files?

Say you have e.g security.config, httphandler.config, errorhandler.config to easily seperate it all.

Would this be possible and how?

Thanks,

John

Thursday, April 10, 2003 7:35 AM by John Mandia

# re: AppSettings can Reference an External Config File

That looks really great Paul.

I have a quick question, can you use this method to define multiple additional config files?

Say you have e.g security.config, httphandler.config, errorhandler.config to easily seperate it all.

Would this be possible and how?

Thanks,

John

Thursday, April 10, 2003 7:35 AM by John Mandia

# AppSettings - External Config File

Monday, September 29, 2003 3:56 PM by TrackBack

# re: AppSettings can Reference an External Config File

Very nice...

Monday, September 29, 2003 4:26 PM by Greg Duncan

# Include extern

Wednesday, October 01, 2003 4:58 PM by TrackBack

# AppSettings can Reference an External Config File

AppSettings can Reference an External Config File

Friday, October 29, 2004 1:34 AM by TrackBack

# re: web.config and App.exe.config for settings JUST DOESNT WORK

Thursday, January 06, 2005 4:51 PM by TrackBack

# re: Binding to a custom App.Config file

Monday, January 10, 2005 7:42 AM by TrackBack

# re: AppSettings can Reference an External Config File

How is this achived in .NET20 using "applicationSettings / MySettings" instead of "appSettings"?

Monday, June 19, 2006 8:07 AM by Benjamin Tengelin

# Error regarding app.config file

Paul, I tried entering the following as per your article in my app.config file: I get the following error msg: {"The root element must match the name of the section referencing the file, 'appSettings' (C:\Program Files\CTGi\CDREngine\app.config line 2)"} System.Exception Can you please tell me what I'm doing wrong?

Thursday, July 13, 2006 2:17 PM by William Yeager

# re: AppSettings can Reference an External Config File

Note that the file you're referencing must have its root be appSettings, not configuration, and there can't be a beginning xml element either.  That's my guess anyhow.

Thanks, Paul Wilson

Thursday, July 13, 2006 2:43 PM by PaulWilson

# re: AppSettings can Reference an External Config File

This is a good example, but honestly, it's really stupid not being able to use an external REAL xml .config file that has the 'xml' tag in it and all other sections and stuff. I have a windows service        

that has its own config file  and i want to use this file from an other Windows application that resides in the same folder, so it looks like this <appSettings file="MyWinService.exe.config"></appSettings> and  what i get is the error: "The root element must match the name of the section referencing the file...". In other words - it is almost useless.

Wednesday, February 14, 2007 3:51 PM by Alex Nilsen

# re: AppSettings can Reference an External Config File

It's great.  I got it to work for a web application that uses a business object (DLL) when I wanted to override the values defined in App.config in Web.config.  Based on my understanding, the advantage of defining values in the Web.config is that it updated every time the content is altered and it doesn't require restart.  Anyways, it's great stuff.

Friday, February 01, 2008 3:24 PM by Vincent

# Phil Bernie Consulting &raquo; 7 Things That Will Make You a Better ASP.NET Web Developer

Pingback from  Phil Bernie Consulting &raquo; 7 Things That Will Make You a Better ASP.NET Web Developer

Leave a Comment

(required) 
(required) 
(optional)
(required)