[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How to write to the app.config file?

Posted on 2010-09-07
4
Medium Priority
?
465 Views
Last Modified: 2012-05-10
I've got a windows forms application and I would like to save a string in the app.config file.

I've googled this and can't find any example code that works.

All I want to do is read a string in when I load my form, and save it after a particular action. The idea is just to persist a setting on the form so the user doesn't have to keep re-entering it each time the form opens.

If I set the value manually I find the string can be read with no problems, but trying to write it just doesn't seem to save it.

Here is my settings file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="LastSearchPath" value=""/>
  </appSettings>

Here is my current code:

            System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);            
            config.AppSettings.Settings["LastSearchPath"].Value = txtPath.Text;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");

0
Comment
Question by:purplesoup
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 22

Assisted Solution

by:Om Prakash
Om Prakash earned 320 total points
ID: 33616836
Please check the following article:
http://www.codeproject.com/KB/cs/SystemConfiguration.aspx
0
 
LVL 12

Accepted Solution

by:
w00te earned 1600 total points
ID: 33616855
I think you're looking to use settings instead of the app.config file.  They're a much simpler way of doing what you're asking.  You can pretty much access/set the exact values you're talking about throught he properties namespace (and it will do all the writing for you).  
See the heading: Using Settings at Run Time" Here:
http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx 
-w00te
0
 
LVL 8

Expert Comment

by:Mohit Vijay
ID: 33616880
Why not you try to save settings in a Database table, you can create a tblAppSettings (example) table in Database and once your application loaded, you can retrive all settings from table and store all config values as xml on any physical path, and can retrieve information whenever required.
0
 
LVL 41

Assisted Solution

by:graye
graye earned 80 total points
ID: 33617497
OK, let's start at the beginning... the old way (.Net 1.0 and 1.1) used the appSetting portion of the config file, and the new way (.Net 2.0, 3.0, 2.5, 4.0) uses "sections" and the ConfigurationManager Class
The new way allows you to save only those items that are marked as having "User" scope.  These setting are not written back to the original config file, but instead are written to a location under the users profile.
http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx 
If you really do need to write back to the original config file, then you'll have to use of the examples listed above for some custom code.
 
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

656 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