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 9, 2003 3:51 PM by PaulWilson

Comments

# 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

# 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 1, 2008 3:24 PM by Vincent