Application Configuration Files
How to use Application Configuration Files.
AI
AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Source Code
Upload
In de .NET Framework are no build-in statements<br>
to access INI files. To do this, you must access the<br>
file directly or use the Win32 API's.<br>
<br>
In DotNet we now have Configuration Files.<br>
This are XML files that can be changed as needed<br>
without recompiling the application.<br>
<br>
There are four different kind of configuration files:<br>
- Machine Configuration Files<br>
- Application Configuration Files<br>
- Security Configuration Files<br>
- ASP.NET Configuration Files<br>
<br>
The Application Configuration File is to compare<br>
with an INI file. With one big difference that<br>
you only can Read settings, but not Write settings!<br>
Writing has to be done manualy.<br>
<br>
<br>
To use a configuration file you must add an<br>
'Application Configuration File' to your project.<br>
<br>
In the Visual Studio Environment this can be done by<br>
right clicking the project, choose 'add new item'.<br>
Then look for the item 'Application Configuration File'.<br>
This will add an 'App.Config' file to your project.<br>
<br>
<?xml version="1.0" encoding="utf-8" ?><br>
<configuration><br>
</configuration><br>
<br>
To add your own settings, you must add a section<br>
called 'appSettings'.<br>
<br>
<?xml version="1.0" encoding="utf-8" ?><br>
<configuration><br>
<appSettings><br>
</appSettings><br>
</configuration><br>
<br>
Here you can store your settings as key/value pairs.<br>
<br>
<?xml version="1.0" encoding="utf-8" ?><br>
<configuration><br>
<appSettings><br>
<add key="Test1" value="This is the value of Test1" /><br>
<add key="Test2" value="This is the value of Test2" /><br>
</appSettings><br>
</configuration><br>
<br>
!!! BEWARE.......THE TEXT IS CASE SENSITIVE !!!<br>
<br>
'appSettings' is not the same as 'appsettings'<br>
Notice the captial S.<br>
A runtime error will occure if you make a typing error.<br>
<br>
<br>
An VB.NET example to read from the Configuration file.<br>
<br>
Imports System<br>
Imports System.Console<br>
<br>
Module Module1<br>
<br>
Sub Main()<br>
Try<br>
Dim config As Configuration.ConfigurationSettings<br>
<br>
' Read the key:Test2<br>
WriteLine(config.AppSettings("Test2"))<br>
<br>
' To read all key's<br>
Dim key As String<br>
<br>
For Each key In config.AppSettings.AllKeys<br>
WriteLine(key & " -- " & config.AppSettings(key))<br>
Next<br>
<br>
WriteLine("")<br>
<br>
Catch ex As Exception<br>
WriteLine("")<br>
WriteLine(ex.ToString)<br>
<br>
Finally<br>
Read()<br>
<br>
End Try<br>
End Sub<br>
<br>
End Module<br>
<br>
<br>
After compiling the assembly, you also will get<br>
an config file in your bin directory.<br>
<br>
For example: an project called MyTest, results in<br>
MyTest.exe<br>
MyTest.exe.config<br>
<br>
The two files must be placed in the<br>
same directory to work.<br>
Original Comments (3)
Recovered from Wayback Machine