|
|
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.
| |
Download 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 9 submission(s) by this author
|
|
|
Report Bad Submission |
|
|
Your Vote! |
See Voting Log |
|
Other User Comments |
5/15/2002 3:18:07 PM:Shane Bauer Nice work. 5
|
7/9/2002 3:00:49 AM:wEnGwAsHeRe nice work! simple but cool!
|
|
Add Your Feedback! |
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here. |
|