Important alert: (current site time 7/15/2013 7:30:53 AM EDT)
 

article

Application Configuration Files

Email
Submitted on: 5/15/2002 7:43:58 AM
By: D. de Haas 
Level: Intermediate
User Rating: By 11 Users
Compatibility: C#, VB.NET, ASP.NET, C++.NET
Views: 43930
 
     How to use Application Configuration Files.

This article has accompanying files
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				In de .NET Framework are no build-in statements
to access INI files. To do this, you must access the
file directly or use the Win32 API's.

In DotNet we now have Configuration Files.
This are XML files that can be changed as needed
without recompiling the application.

There are four different kind of configuration files:
- Machine Configuration Files
- Application Configuration Files
- Security Configuration Files
- ASP.NET Configuration Files

The Application Configuration File is to compare
with an INI file. With one big difference that
you only can Read settings, but not Write settings!
Writing has to be done manualy.


To use a configuration file you must add an
'Application Configuration File' to your project.

In the Visual Studio Environment this can be done by
right clicking the project, choose 'add new item'.
Then look for the item 'Application Configuration File'.
This will add an 'App.Config' file to your project.

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

To add your own settings, you must add a section
called 'appSettings'.

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

Here you can store your settings as key/value pairs.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Test1" value="This is the value of Test1" />
<add key="Test2" value="This is the value of Test2" />
</appSettings>
</configuration>

!!! BEWARE.......THE TEXT IS CASE SENSITIVE !!!

'appSettings' is not the same as 'appsettings'
Notice the captial S.
A runtime error will occure if you make a typing error.


An VB.NET example to read from the Configuration file.

Imports System
Imports System.Console

Module Module1

Sub Main()
Try
Dim config As Configuration.ConfigurationSettings

' Read the key:Test2
WriteLine(config.AppSettings("Test2"))

' To read all key's
Dim key As String

For Each key In config.AppSettings.AllKeys
WriteLine(key & " -- " & config.AppSettings(key))
Next

WriteLine("")

Catch ex As Exception
WriteLine("")
WriteLine(ex.ToString)

Finally
Read()

End Try
End Sub

End Module


After compiling the assembly, you also will get
an config file in your bin directory.

For example: an project called MyTest, results in
MyTest.exe
MyTest.exe.config

The two files must be placed in the
same directory to work.

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 11 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/15/2002 3:18:07 PMShane Bauer

Nice work. 5
(If this comment was disrespectful, please report it.)

 
7/9/2002 3:00:49 AMwEnGwAsHeRe

nice work! simple but cool!
(If this comment was disrespectful, please report it.)

 
1/17/2004 12:05:25 PM

Cool article, though it could be more practical. Keep up the good work
(If this comment was disrespectful, please report it.)

 
8/3/2004 12:35:52 PMJPlumey

Good job, but this doesn't address how to access config settings in a web.xml file.
(If this comment was disrespectful, please report it.)

 
9/21/2005 7:42:49 AMTomas Fridén

Nice job! ConfigurationSettings is a bit "secret" in the help file. Thanks

(If this comment was disrespectful, please report it.)

 
3/25/2012 3:24:10 AMNAYAK

Good
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.