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.

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

  <add key="KeyToOverride" value="Overridden" />
  <add key="KeyToBeAdded" value="EntirelyNew" />


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

  • 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

  • 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 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.

  • 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.

Comments have been disabled for this content.