External AppSetting file for your AppSettings in .config file

Posted by in ASP.NET category on for Beginner level | Views : 39181 red flag
Rating: 4.67 out of 5  
 3 vote(s)

This article describes how to specify an external AppSetting file from within web.config file and specify the settings value inside external file. It also describes related benefits and limitation of the external AppSetting file.


Despite the fact that specifying external AppSetting file in the web.config file was available since the first version of .NET, it was not well known and used. In this article, we will see how to use External .config file to specify AppSetting keys and values.


Video of this article

 You can watch the video of this article at http://www.dotnetfunda.com/tutorials/videos/x70-external-appsetting-file-for-your-appsettings-in-config-file-.aspx


Specifying the External AppSetting file

In order to specify the external AppSetting file, you will have to use the file attribute of the AppSettings node.

<appSettings file="MyConfig.config">

<add key="MyKey" value="12"/>


In the above code snippet, MyConfig.config file is my external config file that I can use to specify the key value pairs for my AppSetting.

Where can I use this?

You can use Exernal AppSetting file to separately maintain your AppSettings in order to avoid complexity of maintaining everything into a single web.config or app.config file. You can also use this external setting file to distinguish your settings between different development, staging and production environments.

Point of interest about External AppSetting file

  • You can keep your appSettings keys and values both in your web.config or app.config or your external AppSetting file.
  • If you specify the same key into your web.config file and external AppSetting file (MyConfig.config in my case, the web.config key value will be overridden by the external AppSetting file key value.
  • The extension name of the external AppSetting file need not be a .config, you can specify .xml or .config1 etc as well. However it is suggested to keep the extension name as .config as .config is protected from external world and it can't be downloaded.
  • You will not be able to specify more than one external AppSettings file from your web.config file, as duplicate settings nodes inside web.config is not allowed.
  • You can not specify another external AppSetting file from within the first external AppSetting file (for example: I can't specify another external AppSetting file MyConfig1.config file from MyConfig.config file). If you do that, you will get following error.
Unrecognized attribute 'file'. Note that attribute names are case-sensitive. (C:\Mukul\WebSite1\MyConfig.config line 2)
  • If you modify the key value of your external AppSetting file while application is running, the change will not get affected unless you restart your application.
  • The external AppSetting config file must contain only AppSetting node or you will get an error.
  • You can keep this External AppSetting file in any of the subfolder and you can specify the path something like below. Here, I have kept my conifg file into Config folder under root.
Get solutions of .NET problems with video explanations, .pdf and source code in .NET How to's.

<appSettings file="Config/MyConfig.config">

How to specify appSettings in the External AppSetting file?

You can specify the appSettings keys and values into your External AppSetting file in the same way, you specify in the web.config or app.config file. Below is the code snippet of my external appSetting config file.

<?xml version="1.0"?>


<add key="MyKey1" value="MyValue1 - coming from Exernal MyConfig.config"/>


How to retrieve values of the external AppSetting file?

Easy and no need any extra effort, just retrieve it the way you retrieve from your appSettings inside web.config or app.config file. Of course, as usual you will have to use using System.Configuration; namespace.

protected void Page_Load(object sender, EventArgs e)


// get from web.config

string myKey = ConfigurationManager.AppSettings.Get("MyKey");

lblMessage.Text += "<b>AppSetting value from web.config: </b>" + myKey;


// get from external AppSetting file

myKey = ConfigurationManager.AppSettings.Get("MyKey1");

lblMessage.Text += "<br /><b>AppSetting value from external AppSetting file: </b>" + myKey;


In the above code snippet, first time myKey variable is set with web.config appSetting key value and second time it is set with external MyConfig.config file appSetting key value.


Hope you like this article small article on external AppSetting file. Keep learning and sharing your knoweldge on this website.

 You can watch the video recording of this topic at http://www.dotnetfunda.com/tutorials/videos/x70-external-appsetting-file-for-your-appsettings-in-config-file-.aspx

Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan http://www.dotnetfunda.com

Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on http://www.facebook.com/sheo.narayan | https://twitter.com/sheonarayan | http://www.linkedin.com/in/sheonarayan

Login to vote for this post.

Comments or Responses

Posted by: Poster on: 7/31/2010
I didn't know about it that we can even specify external AppSetting file. Its cool and hardly known by people.

Thanks for sharing this.
Posted by: Rama_aluri on: 1/25/2011 | Points: 25
ur article is good one. According ur article, ur creating one external config file.ok it works well . but iam trying to read key values from more than one external file ,i am getting error.
So Please tell me how to read key values from more than one external file.
asp.net website supports to create no of config files.but i am unable to read key value from those files.

Posted by: Bharatkrjangir on: 10/15/2013 | Points: 25

Can we put connectionstrings on this custom config file ?

<add name="" connectionString="Database=TestDB;Server=localhost;Integrated Security=false;User ID=Test;Password=Test;" providerName="System.Data.SqlClient" />

If yes, than what is the syntax to access these key-value?

Login to post response

Comment using Facebook(Author doesn't get notification)