Click here to Skip to main content
13,295,616 members (27,755 online)
Click here to Skip to main content
Add your own
alternative version


82 bookmarked
Posted 26 Mar 2004

Configuration Settings File for providing application configuration data

, 26 Mar 2004
Rate this:
Please Sign up or sign in to vote.
This article talks about the 'Application Configuration' File in .NET and provides sample code to read from the config file.


.NET gives an easy way to store configuration information in an Application Configuration File. In the simple implementation, you can store information as Key-Value pairs.

For example, consider a case where you have to use a data source in your application. If you hardcore the data source information in your code, you will have a bad time when you have to change this data source. You have to change your source code and re-compile it. This won't work every time you give your product to different customers or when you run your application in different machines!

In earlier days, programmers used to store this information in special files called .ini files or in system registry. The application can read the information from the .ini file or registry and no need to re-compile the code when the values are changed in .ini file or registry.

But this is a pain most of the time. It is not fun opening the registry, locate your entries and make appropriate changes. It is quite possible that you may mess up with some important entries in the registry and make your system not run any more. In fact, in secured systems, administrator may deny access to the registry and users will not have the choice to edit the registry at all.

.NET gives you a simple and easy solution for this problem - the Application Configuration File. Each application can have a configuration file, which is actually an XML file. You can use any text editor (including Notepad) to open the configuration file and change the values. The application will load the values from this configuration file and you do not have to change your source code every time you change your data source or any other information stored in the configuration file.

app.config for Windows applications

Windows applications in VS.NET use the name app.config by default for the configuration file. This will not be automatically created when you create a Windows application. If you need a configuration file for your application, open your project in VS.NET, go to the 'Solution Explorer' and right click on the project name. Choose Add > Add new item from the menu and select 'Application Configuration file' from the list of choices. This will create an app.config file for you in the application root.

By default, the app.config file will have the following content:

<?xml version="1.0" encoding="utf-8" ?> 



To store values in the configuration file, you can create XML elements in the format:

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

See the sample config entries below:

<?xml version="1.0" encoding=    "utf-8" ?>



    <add key="DatabasePath" value="c:\\projects\data\spider.mdb" />

    <add key="SupportEmail" value="" />



And to read from this config file, just use the following code in your application:

string dbPath = 
string email = 

ConfigurationSettings is the class used to access the contents of the configuration file. Since this class is part of the namespace System.Configuration, we have to use the fully qualified name System.Configuration.ConfigurationSettings. As a shortcut, you can use the using directive on top of the file like below:

using System.Configuration;

If you have the above directive on top of the file, then you can directly use the class ConfigurationSettings.

string dbPath = ConfigurationSettings.AppSettings["DatabasePath"];
string email = ConfigurationSettings.AppSettings["SupportEmail"];

In VB.NET, you have to use "( ... )" instead of the "[ ... ]", as shown below:

Dim dbPath as String = ConfigurationSettings.AppSettings("DatabasePath")
Dim email as String = ConfigurationSettings.AppSettings("SupportEmail")

Note: When you compile your application, VS.NET will automatically create a file called <your application name>.exe.config in your bin\debug folder. The contents of the app.config will be automatically copied to this new config file when you compile the application. When you deliver the application to the end user, you have to deliver the exe and this new config file called <your application name>.exe.config and NOT the app.config. Users can modify the data in <your application name>.exe.config file and application will read the data from the config file, when restarted.

web.config for Windows applications

The web applications use the same concept, but they use a config file with the name web.config. There are couple of things to note in this case.

  • web.config is created automatically by VS.NET when you create any web project.
  • When you compile the web application, web.config is NOT renamed or copied to the BIN folder.
  • web.config has several default entries in it to support web/IIS configuration & security.
  • You can add the <appSettings> section in the web.config and add your key/value pairs in that section.
  • You can have separate web.config files for each directory in your web application, in addition to the one in the root. For each web page, by default, system will look for a web.config in the same folder as the page and if not found, then looks in the parent folder.


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

T Manjaly
Web Developer
India India
Tony is a seasoned .NET developer who recently switched his focus to Windows 8 and SEO. You may visit his technology websites: and

You may also be interested in...


Comments and Discussions

QuestionConfiguring during setup Pin
Member 429190615-Mar-17 0:14
memberMember 429190615-Mar-17 0:14 
GeneralMy vote of 3 Pin
Paul Sincai30-May-14 2:45
memberPaul Sincai30-May-14 2:45 
GeneralRe: My vote of 3 Pin
Member 1135318618-May-15 23:43
memberMember 1135318618-May-15 23:43 
GeneralMy vote of 4 Pin
emory reid6-Apr-13 3:46
memberemory reid6-Apr-13 3:46 
GeneralMy vote of 5 Pin
michael wondimu16-Aug-12 23:54
membermichael wondimu16-Aug-12 23:54 
QuestionAppication Configuration - connection string Pin
puchie25-Jul-11 18:03
memberpuchie25-Jul-11 18:03 
GeneralMultiple projects and one settings config file Pin
elitefox23-Jan-11 15:56
memberelitefox23-Jan-11 15:56 
GeneralSolves my problem Pin
mayuri12315-Apr-08 20:53
membermayuri12315-Apr-08 20:53 
GeneralRe: Offshore Software Development Pin
Pete O'Hanlon7-Sep-07 4:50
memberPete O'Hanlon7-Sep-07 4:50 
GeneralApp Config Pin
Stixoffire224-Aug-06 11:06
memberStixoffire224-Aug-06 11:06 
GeneralRe: App Config Pin
marklsdev9-Oct-06 3:10
membermarklsdev9-Oct-06 3:10 
GeneralA great article Pin
plaster23-Feb-06 23:08
memberplaster23-Feb-06 23:08 
GeneralRe: A great article Pin
llcoollasa1-Apr-09 23:23
memberllcoollasa1-Apr-09 23:23 
GeneralRe: A great article Pin
Sreerenj21-Jul-09 6:07
memberSreerenj21-Jul-09 6:07 
QuestionCan config file store HTML value? Pin
yancyn1-Feb-06 21:25
memberyancyn1-Feb-06 21:25 
AnswerRe: Can config file store HTML value? [modified] Pin
mr_mark_hewitt18-Jul-06 3:08
membermr_mark_hewitt18-Jul-06 3:08 
GeneralBlock Modify Pin
Vitoto7-May-05 10:12
memberVitoto7-May-05 10:12 
GeneralFound the problem Pin
Mumita19-Apr-05 0:08
sussMumita19-Apr-05 0:08 
Generalhelp Pin
Mumita15-Apr-05 3:03
sussMumita15-Apr-05 3:03 
GeneralRe: help Pin
Anonymous15-Apr-05 4:48
sussAnonymous15-Apr-05 4:48 
GeneralRe: help Pin
Mumita15-Apr-05 5:58
sussMumita15-Apr-05 5:58 
GeneralRe: help Pin
Mumita15-Apr-05 6:00
sussMumita15-Apr-05 6:00 
GeneralMany Thanks Pin
Don Cassas11-Mar-05 6:29
memberDon Cassas11-Mar-05 6:29 
GeneralThanks ! Pin
Lim Bio Liong5-Dec-04 0:46
memberLim Bio Liong5-Dec-04 0:46 
GeneralMissing info Pin
Steven Campbell28-Mar-04 14:22
memberSteven Campbell28-Mar-04 14:22 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 27 Mar 2004
Article Copyright 2004 by T Manjaly
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid