Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Application Configuration Files 1

Status
Not open for further replies.

wsnider

Programmer
Apr 29, 2002
15
0
0
US
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.

For example, a config file:

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<configuration>
<appSettings>
<add key=&quot;dbServer&quot; value=&quot;srv-sql-003&quot;/>
</appSettings>
</configuration>

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(&quot;dbServer&quot;)

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=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<configuration>
<appSettings>
<add key=&quot;TestStr&quot; value = &quot;TestStr&quot; />
</appSettings>
</configuration>

My application code to get this string is then:
Dim strConn As String
strConn = System.Configuration.ConfigurationSettings.AppSettings(&quot;TestStr&quot;)


It never works though; it is always nothing.


Any suggestions?

James Goodman MCSE, MCDBA
 
Make sure that the config file has the correct file name i.e. 'appname.exe.config'.

For example, if your app is called Word, the config file would be called 'Word.exe.config'.
 
If you add the app.config file in your project before you compile, it will take care of the naming for you.
 
&quot;If you add the app.config file in your project before you compile, it will take care of the naming for you.&quot;

Where do I get this from?

I created a file called app.config & added it to my source directory, but when I compile the app appname.exe.config is not created...

James Goodman MCSE, MCDBA
 
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(&quot;ConnStr&quot;)

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.



Any suggestions as to what this means?

James Goodman MCSE, MCDBA
 
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

doc.Load(&quot;ConfigFile.xml&quot;)
root = doc.DocumentElement

book = root.SelectSingleNode(&quot;/YourXML/YourSetting&quot;)

Return = book.InnerText

Thats what I use to retreive my values.

hth

D'Arcy
 
Yeah... when using quotations in your app.config file, it get's a little tricky.

Replace the quotation marks (&quot;) with (&quot;) and the built in appsettings method will read it properly.

Example...

Password=&quot;&quot;;User ID=Admin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top