Saving app settings - What's the opposite of appsettingsreader?

D

David

I just tried out, for the first time, an app.config file for a Windows forms
app. What could be simpler? Edit the file and save it, then create an
System.Configuration.AppSettingsReader object. Write a line of code for the
setting to retrieve and, done! Fantastic.

So, I thought, there is probably an AppSettingsWriter class. No? No. I
looked through the System.Configuration namespace, and saw lots of ways to do
what I wanted to do, but none of them jumped out at me as the best way to do
it with the least amount of coding.

All I want to do is store a couple of settings that the user makes during
program execution. For example, my particular program can run three copies
of equipment, so the user has to select which one he is using. However,
after the first time he does this, the computer stays with the machine, so I
would like to save it as the default, and read it out of the app.config file,
so he doesn't have to select it next time. Is there a "lazy programmer's
guide to using the app.config file for storing user data"?
 
M

miher

Hi,

For storing user setings You can use the built-in support for that. (Go
project properties, then select the settings tab). I think its much easier
way of storing settings than in app.config.

If You insist on writing to app.config, this is one possible way to do it:
Configuration c =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
c.AppSettings.Settings.Add("newKey", "newValue");
c.Save();
// ConfigurationManager.RefreshSection("appSettings"); for
refresh

Hope You find this useful.
-Zsolt
 
H

Hongye Sun [MSFT]

Hi David,

Thanks for using Microsoft Newsgroup Service. My name is Hongye Sun [MSFT]
and it is my pleasure to work with you and zsolt on this issue.

First, I would want to thank zsolt's accurate and complete answer. Project
settings and Configuration.AppSettings.Settings are two solutions to your
problem.

AppSettingsReader is a shortcut class to read the AppSettings, however
there is no shortcut to write in .NET Framework. It reads data from
ConfigurationManager.AppSettings which is also a shortcut to get the
current AppSettings.

Project Settings contains application settings and user settings.
Application settings is readonly. It is saved at
<ApplicationName.exe>.config file and its section is
<applicationSettings>/<ApplicationName.Properties.Settings>/<setting>. It
usually saves data which can only be changed by modifying config file.

User settings can be read-writable, which is saved in user's folder
(%AppData%). The settings usually saves user specific data, for example:
user's preference. It is differs with different users. This one seems to be
what you want.

Configuration.AppSettings.Settings.Add and
Configuration.AppSettings.Settings[<SettingName>].Value can be used to add
and change existing settings of AppSettings, which can be read through
AppSettingsReader. This is also application specific. If one user change
the setting, it will impact other users. Please be careful that if you run
Configuration.AppSettings.Settings.Add to add settings multiple times. It
will not add override the specific setting's value. Instead, it will
accumulate the values like: <add key="key1"
value="value1,value2,value3,value4" />

The code below can solve this problem:
Configuration c =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

if (c.AppSettings.Settings.AllKeys.Contains("key1"))
{
c.AppSettings.Settings["key1"].Value = "newvalue";
}
else
{
c.AppSettings.Settings.Add("key1", "newvalue");
}

c.Save(ConfigurationSaveMode.Modified);


Please let us know if Zsolt and my answers solve your problem. Thanks.

Have a nice day.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top