Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Working with Application configuration file in .NET 2.0 using C#.

Author: DevAsp Team
Download Source Code : 679_Application

In this simple article you will learn how Create an Application configuration file and read data from it.

If we want to develop some configurable application (can be configure after deployment) then we use Application configuration file. These are similar to .ini files.


The System.Configuration namespace provides the functionality for reading configuration files.


It has different sections














simply create an C# Windows application.

Now add application configuration file from Add new item menu.


Write this code in app.config file


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





            <add key="ApplicationTitle" value="DevAsp Application Configuration Sample" />

            <add key="ApplicationHeaderText" value="DevAsp" />






            <add name="ConnectionString" connectionString="user id=DevAsp;data source=Northwind;persist security info=True;initial catalog=Comments;password=abc"

            providerName="System.Data.SqlClient" />






There are two values in appSetting section and one string value in connectionString Section.


First add reference of System.Configuration in References.


Now simple drag two labels, one button and one RichTextBox on form1. Now write following code on these events.

private void Form1_Load(object sender, EventArgs e)


            this.Text = ConfigurationSettings.AppSettings["ApplicationTitle"];


            label3.Text = ConfigurationSettings.AppSettings["ApplicationHeaderText"];




        private void button1_Click(object sender, EventArgs e)


            richTextBox1.Text = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();


            richTextBox1.Text += "\n\n" + ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName.ToString();



Now you see that it is very simple to use.


Now when you change key value from debug folder and run application from debug it will show the the changed values. It shows that you can change the values after deployment of your product.


Article Comments
Sir my database connection is in config file now I change the database and user name and password of that config file what is the procedure to perform these task ,sir please give me a solution of this problem as soon as posible

Posted on 8/6/2007 3:27:56 AM by mohsin

I think
is obsolete:

Instead, use:

if (null != ConfigurationManager.AppSettings["CoolGuyKey"])
string coolGuy = ConfigurationManager.AppSettings["CoolGuyKey"];

Posted on 2/25/2008 3:52:35 PM by GC

Getting an error like "Object Reference not set to an instence of an object".

What to do?

Posted on 4/11/2008 4:36:57 AM by mayuri

isent there anyway to make configutaion properties programmaticly??

Posted on 4/19/2008 6:46:15 PM by Charpx

i m new to .net. i want to learn .net(C#,asp). can u suggest me a links or a book which is specified for a rookie as yashwant kanitkar for C which can be easily understood. plz mail me

Posted on 4/23/2008 1:58:52 AM by Infiniti

Hi GC,

I think you are write by saying that ConfigurationSettings is obsolete but ConfigurationManager does not show in MS Studio 2005 in some versions, so there is no harm is using ConfigurationSettings in that case.

Posted on 7/11/2008 9:24:31 PM by Sanchay Subhedar

Hi Moshin,

I think having database in your config file is not a very good idea. Instead connect your database to your application.

Posted on 7/11/2008 9:25:54 PM by Sanchay Subhedar

) Open MS Visual Studio

2) File->New file-> General->Textfile

3) After a blank screen is in front of you Save it as "Your application name".exe.config in bin/debug folder of your application.

4) code your config file.

for ex

CAUTION-> REPLACE All "(" with smaller-than sign (shift+,) and ")" with greater-than (shift+.) sign in following code. Blog is not allowing me to post it that way

(?xml version="1.0" encoding="utf-8" ?>
(add key="Key1" value="Kevin" /)
(add key="Key2" value="150" /)
(add key="Key3" value="Rice" /)

5) save it using ctrl-S

Then in your Solution Explorer, you can see the config file by the name "your application name".config

Now in your main application, you can access the config file.

1) Add using System.Configuration namespace.

2)static void Main(string[] args)
string str1 = System.Configuration.ConfigurationSettings.AppSettings["Key1"];
string str2 = System.Configuration.ConfigurationSettings.AppSettings["Key2"];
string str3 = System.Configuration.ConfigurationSettings.AppSettings["Key3"];
System.Console.WriteLine("test1="+str1+", test2="+str2+", test3="+str3+".");


If program compiles and runs successfully, you can see all the three strings.

To convert integer to string use parse function of integer class

int i = int.Parse(str2);

So, isnt it simple to create a config file?????

I was terribly afraid when my team member said, make a config file because I had never made, one before!!

post your questions, if any!!


Posted on 7/11/2008 9:29:23 PM by Sanchay Subhedar

[ConfigurationManager does not show in MS Studio 2005]
- You most add a reference to System.Configuration to use ConfigurationManager

[- 2) File->New file-> General->Textfile ]
- Youn can use the template of vs2005, ->Proyect->Add NewItem->Application Configuration File

[isent there anyway to make configutaion properties programmaticly??]
- Tou must open the configuration file usign Xml
XmlDocument doc = new XmlDocument()

Posted on 8/13/2008 2:14:31 PM by ichramm

This is comprehansive help for those who are new to .Net and want to avoid hard-coded stuff. Adding keys in Application Config file solves a lot of matters.

Posted on 9/16/2008 12:12:05 AM by Kashif Hussain

This article was very nice but I have one dout i;e
ConfigurationManager does not show in MS Studio 2005 in some versions,
In which version that will be showing plz tel me sir Iam waiting ur reply.........
Thank u sir...

Posted on 9/16/2008 10:58:09 AM by Leela

Its Cool....
But What Can I Do When I Want To Change My Connection String Dynamicaly on a different System Without Changing The Connection String In The Configuration file...
So Please Help Me...

Posted on 1/15/2010 3:26:35 AM by Brahmananda Swain

Sir i have included an acces file in my project and deployed it
but when i run it on diff machine an error is coming tht it is not able to find the database file due to the change in the address of the file. Now how to make the application locate the file in the other system with the hellp of a config file.

Posted on 3/6/2010 2:38:29 AM by Vineet More

Configuration Manager is not showing in MS Studio 2005 in versions,
plz help me out.........!

Posted on 4/22/2010 3:20:43 AM by John Smith

I want that this configuration file should also work on the applications where the controls like TextBox, Lable, ComboBox has been binded with the DataSource & work on any computers having deferent server name (SQL) from each other

Posted on 5/23/2010 3:04:13 AM by Bhavin Patadia

Add Article Comment:
Name :
Email Address :
Comments :
<< Working with onSelectedIndexChange Event in DropDownList

Disclaimer - Privacy
© 2002-2018