Solved

How to write to the app.config file?

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now