Solved

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

Posted on 2006-07-21
13
305 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Obtain cell value using column ID in DataGrid using VB.NET 2 25
System.net.Sockets Error 5 28
Vb.net threads keep increasing 2 36
How to repeat the data 4 18
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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