Solved

How to write to the app.config file?

Posted on 2010-09-07
4
456 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
4 Comments
 
LVL 22

Assisted Solution

by:Om Prakash
Om Prakash earned 80 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 400 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 20 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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