Solved

I am having a problem being able to set and read values in the app.config file.

Posted on 2006-07-21
13
296 Views
Last Modified: 2010-04-23
I'm using VS 2005, and as far as I can tell, the app.config wizard does nothing.  I want to be able to set a number of key/value settings in the config file, however, I cannot *read* in code, anything I set.  And I am no longer even sure what code I am supposed to use.

Could someone show me both an app.config file with some key/value pairs, and the code in a class library to read and write to this config file?  500 points for anyone that can show me code that I can see the proper way to read and set the settings in app.config and the code to read and update the settings.  

Thank you so much for your help.
0
Comment
Question by:louisiana_blues
  • 6
  • 3
  • 3
  • +1
13 Comments
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17155653
Use My.Settings to access your settings in VS2005.

Right-click on your project name in solution explorer, and select properties (right at the botton). The project properties will open. Select the settings tab. Create your settings, and set them as either user or application settings.

Then, you can access them using My.Settings.<setting name>
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17155910
to read values, import System.Configuration and then do

dim value as string= ConfigurationManager.AppSettings(key)
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17155943
to write

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)      
config.AppSettings.Settings.Add("key", value)    
config.Save(ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection("appSettings")
0
 

Author Comment

by:louisiana_blues
ID: 17155986
Hi YZlat.  Thank you so much for responding.

So, if I had a database connection string, what would be the standard way of reading this in from an app.config file?  Could you give me a specific example?  You are very close, but while I now understand the theory, I really need a specific example, since I always end up botching up the xml or something...
0
 

Author Comment

by:louisiana_blues
ID: 17156057
And also, your code infers that there is something like an 'application settings' part of my app.config.  I do not see one when I create an app.config file from vs 2005.  Just another reason why I would really need an example of the app.config file, with something like a 'connection string' for a database stored in the file...I hope you can help...
0
 
LVL 24

Assisted Solution

by:Jeff Certain
Jeff Certain earned 150 total points
ID: 17156140
Not to be difficult about this... but My.Settings actually writes to app.config. The whole point of My.Settings was to make it easier to interact with app.config from code.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 100 total points
ID: 17156198
If you are using 2005 and want to get a connection string from the connectionStrings section:

  <configuration>
    <connectionStrings>
      <add name="MyConnString" connectionString="YadaYada"/>
    </connectionStrings>
  </configuration>

Then use:

    Dim connStr As String = ConfigurationManager.ConnectionStrings("MyConnString").ConnectionString

Don't forget to add a reference to the System.Configuration assembly and add an Imports System.Configuration directive.
0
 
LVL 35

Accepted Solution

by:
YZlat earned 250 total points
ID: 17156251

your config file should look something like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
      <appSettings>
            <add key="ConnectionString" value="your connection string here" />
      </appSettings>
</configuration>

then do

dim strConn as string= System.Configuration.ConfigurationManager.AppSettings("ConnectionString")
0
 

Author Comment

by:louisiana_blues
ID: 17156310
Okay.  Thanks YZlat!  I will try this, and if it works you get the points!
0
 

Author Comment

by:louisiana_blues
ID: 17157760
That worked wonderfully YZLat.  Thank you so much for you patience and your help!  A definite 'A'!
0
 

Author Comment

by:louisiana_blues
ID: 17157769
Whoa, I did a refresh and saw Chaosian and Carl added some wisdom here.  Well, I shall share then.  I truly appreciate you all.  You've been very helpful today.
0
 

Author Comment

by:louisiana_blues
ID: 17157789
I tried to be as fair as possible.  Sometimes it's hard to figure out exactly what points someone should get.  I wish I could give 500 pts to each of you...but Chaosian's mention of the My.Configuration, was indeed helpful, though YZLat also gave me some very helpful example code, as did Carl.  A conundrum of how to divvy up points.  You've had a 'sharing' day Chaosian.  Sorry about that! :)
0
 
LVL 24

Expert Comment

by:Jeff Certain
ID: 17158013
No problem -- with such great experts here, I often find myself struggling even to add a little bit of wisdom. Sometimes, it's all I can do to add an extra viewpoint :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now