Solved

How to write to the app.config file?

Posted on 2010-09-07
4
457 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 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Universal App 22 58
Variable 'id' is used before it has been assigned a value 29 44
What is needed to become a DBA? 7 53
SQL State HYT00. Timeout expired proplem 8 43
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

734 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