Solved

How to write to the app.config file?

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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

695 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