Solved

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

Posted on 2006-07-21
13
301 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Paging GridView 7 48
VB.Net - For Loop Error 5 32
Closing all open child forms. 4 38
POWERSHELL: do / until, should continue even when there is an error 4 51
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

919 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

16 Experts available now in Live!

Get 1:1 Help Now