Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 325
  • Last Modified:

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

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
louisiana_blues
Asked:
louisiana_blues
  • 6
  • 3
  • 3
  • +1
3 Solutions
 
Jeff CertainCommented:
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
 
YZlatCommented:
to read values, import System.Configuration and then do

dim value as string= ConfigurationManager.AppSettings(key)
0
 
YZlatCommented:
to write

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)      
config.AppSettings.Settings.Add("key", value)    
config.Save(ConfigurationSaveMode.Modified)
ConfigurationManager.RefreshSection("appSettings")
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
louisiana_bluesAuthor Commented:
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
 
louisiana_bluesAuthor Commented:
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
 
Jeff CertainCommented:
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
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
YZlatCommented:

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
 
louisiana_bluesAuthor Commented:
Okay.  Thanks YZlat!  I will try this, and if it works you get the points!
0
 
louisiana_bluesAuthor Commented:
That worked wonderfully YZLat.  Thank you so much for you patience and your help!  A definite 'A'!
0
 
louisiana_bluesAuthor Commented:
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
 
louisiana_bluesAuthor Commented:
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
 
Jeff CertainCommented:
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

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!

  • 6
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now