Application Configuration Files

Submitted on: 1/1/2015 4:37:00 AM
By: D. de Haas (from psc cd)  
Level: Intermediate
User Rating: By 11 Users
Compatibility: C#, VB.NET, ASP.NET, C++.NET
Views: 5487
     How to use Application Configuration Files.

This article has accompanying files
				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" ?>

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

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

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

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


'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()
Dim config As Configuration.ConfigurationSettings

' Read the key:Test2

' To read all key's
Dim key As String

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


Catch ex As Exception


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

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

Other 4 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

 There are no comments on this submission.

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.