I am looking for some good advice and/or code on using ".config" files instead of ".ini" files for data external to an application such as paths, dates, etc.
I use config files to store variables that may change (such as a server path). It means that I can replace the config file with a new value without having to recompile the application.
If the database ever moves from machine srv-sql-003 to another, all I need to do is change the value in the config file and the app will point to the new server location.
You can retrieve values from the config file using:
Dim s As String = System.Configuration.ConfigurationSettings.AppSettings("dbServer"
The config file is also used for dynamic URL's on web services - if you have a reference to a web service and the URL path is set to dynamic, if the web service moves you only need to change the path in the config file, not recompile the app with a redirected proxy.
Thanks SHelton for the simple straightforward explanation and example. There is little on this subject in books, etc. Helton's example file and the DIM statement are clear as glass.
This isnt working on my app for some reason. My app.config file is stored in the app directory & is as follows:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="TestStr" value = "TestStr" />
</appSettings>
</configuration>
My application code to get this string is then:
Dim strConn As String
strConn = System.Configuration.ConfigurationSettings.AppSettings("TestStr"
Ok, I was wrong. It will only work once for some reason.
The code I am using to retrieve the value is:
'Modify the connection string if an alternate is specified
Dim strConn As String
strConn = Configuration.ConfigurationSettings.AppSettings("ConnStr"
However, when I try to retrieve it I get the error:
An unhandled exception of type 'System.Configuration.ConfigurationException' occurred in system.dll
Additional information: This is an unexpected token. Expected 'NAME'. Line 4, position 77.
The way they've suggested is very intriguing, and I'm going to check it out.
If you're having problems with strings though, and you want to get this working, here's what I've been doing (the same thing, just manually reading in the xml):
Dim doc As XmlDocument = New XmlDocument
Dim book As XmlNode
Dim root As XmlNode
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.