Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-07-21
13
Medium Priority
?
313 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 600 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
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 400 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 1000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Screencast - Getting to Know the Pipeline

916 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