Saving settings to app.config using C#?

  • Thread starter Jeff Richardson
  • Start date
J

Jeff Richardson

Hi,
How do I save updated values for Application Settings defined with an
Application scope? I created the Application Settings using the Project
Properties dialog in VS2005.

I am trying to create a dialog box where users can save their preferences.
The dialog reads values from the app.config, then update those values. I
want these setting to apply to all users of the application.

I have tried code like the following:
mySettings["Latitude"] = decimal.Parse(txLat.Text);

mySettings["Longitude"] = decimal.Parse(txLon.Text);

mySettings.Save();

The code runs but does not save the settings.

Thanks
Jeff.
 
K

Kevin Spencer

Properties.Settings.Default.Save();

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Development Numbskull

Nyuck nyuck nyuck
 
H

Herfried K. Wagner [MVP]

Kevin Spencer said:
Properties.Settings.Default.Save();

.... won't do anything for application-scoped settings because typically
normal users do not have write rights for the application's directory
containing the configuration file.
 
J

Jeff Richardson

Kevin, thanks for the reply!
I updated my app to the following:
Properties.Settings.Default["Latitude"] = txLat.Text;

Properties.Settings.Default["Longitude"] = txLon.Text;

Properties.Settings.Default.Save();

and the in-memory Properties seem to update OK, but the new values are not
being written to the app.exe.config file. I must be doing something silly.
Any additional help would be appreciated.

Jeff.

Kevin Spencer said:
Properties.Settings.Default.Save();

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Development Numbskull

Nyuck nyuck nyuck


Jeff Richardson said:
Hi,
How do I save updated values for Application Settings defined with an
Application scope? I created the Application Settings using the Project
Properties dialog in VS2005.

I am trying to create a dialog box where users can save their
preferences. The dialog reads values from the app.config, then update
those values. I want these setting to apply to all users of the
application.

I have tried code like the following:
mySettings["Latitude"] = decimal.Parse(txLat.Text);

mySettings["Longitude"] = decimal.Parse(txLon.Text);

mySettings.Save();

The code runs but does not save the settings.

Thanks
Jeff.
 
F

FUnky

The new values are being written into a separate config file stored in the
C:\Documents and Settings\[user]\Local Settings\Application
Data\[application_name] folder. Since they are user specific settings, they
are stored in a user.config file, and not in the app.exe.config file.

You just have to do the reverse thing to fetch them :
txLon.Text = Properties.Settings.Default["Longitude"];


FUnky
 
L

Linda Liu [MSFT]

Hi Jeff,

There're two options to refer to the Properties in application settings in
C#. Suppose there is an application-scoped property named "AppSetting" in
the application settings. I can access this property in two ways as follows.

textBox1.Text = Properties.Settings.Default.AppSetting;
textBox1.Text = Properties.Settings.Default["AppSetting"];

When I want to upate the value of textBox1 to the AppSetting property, it
seems that I have two options to do this as well.

Properties.Settings.Default.AppSetting = textBox1.Text; // when compiled,
an error occurs saying that the AppSetting cannot be assigned to -- it is
read only
Properties.Settings.Default["AppSetting"] = textBox1.Text; // no error
occurs

The first statement will raise an error while being compiled, saying that
the AppSetting cannot be assigned to -- it is read only. The second
statement can pass the compilation. However, even if you can change the
AppSetting property with the second statement at run-time, the new value
cannot be written to disk with the statement
"Properties.Settings.Default.Save()".

To save the AppSetting property to disk, you could only change the
property's type to user-scoped. After you change the value of this property
and save the new value to disk, the new value is stored in a user.config
file. Notice that the default values are still saved in app.config.

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to let me know.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
C

Cris

Sorry and ...
if I've more projects in a VS 2005 solution, how can I write or read
the AppSetting of the application number 1 while I'm in the
application number 2?

Bye & Thanks
Cris


Linda Liu [MSFT] ha scritto:
 
K

Kevin Spencer

The only way to do that would be to open and parse the XML file, as it is
not in the application root directory of the second app.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Development Numbskull

Nyuck nyuck nyuck


Cris said:
Sorry and ...
if I've more projects in a VS 2005 solution, how can I write or read
the AppSetting of the application number 1 while I'm in the
application number 2?

Bye & Thanks
Cris


Linda Liu [MSFT] ha scritto:
Hi Jeff,

There're two options to refer to the Properties in application settings
in
C#. Suppose there is an application-scoped property named "AppSetting" in
the application settings. I can access this property in two ways as
follows.

textBox1.Text = Properties.Settings.Default.AppSetting;
textBox1.Text = Properties.Settings.Default["AppSetting"];

When I want to upate the value of textBox1 to the AppSetting property, it
seems that I have two options to do this as well.

Properties.Settings.Default.AppSetting = textBox1.Text; // when
compiled,
an error occurs saying that the AppSetting cannot be assigned to -- it is
read only
Properties.Settings.Default["AppSetting"] = textBox1.Text; // no error
occurs

The first statement will raise an error while being compiled, saying that
the AppSetting cannot be assigned to -- it is read only. The second
statement can pass the compilation. However, even if you can change the
AppSetting property with the second statement at run-time, the new value
cannot be written to disk with the statement
"Properties.Settings.Default.Save()".

To save the AppSetting property to disk, you could only change the
property's type to user-scoped. After you change the value of this
property
and save the new value to disk, the new value is stored in a user.config
file. Notice that the default values are still saved in app.config.

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't
hesitate
to let me know.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
J

Jeff Richardson

Linda,
Thank you for your detailed response!

So are you saying that there is no way to update and application scoped
property? That seems somewhat limiting since some settings should apply to
all users and other settings should be scoped to the user. In my example,
the location of the computer as defined by it's Latitude and Longitude
should apply to the whole computer, where as other settings like a user
preference on the application's window size and location should be scoped to
a particular user.

Is it possible to update an application scoped property by loading the
exe.config file as an XML file and updating it using the XML DOM? If it can
not be done, what are my alternatives?

Thanks in advance!
Jeff.
 
L

Linda Liu [MSFT]

Hi Jeff,

Thank you for your response.

No, we could not save the new value of an application-scoped setting to
disk at run time by the statement "Properties.Settings.Default.Save()".
However, we could do this regarding the exe.config file as an XML file by
using XML DOM.

Here is a sample for you. I put two textboxs and a button on the form. When
the form is loaded, the values of the latitude and longtitude settings in
exe.config file are read and displayed in the two textboxs. When I press
the button, the new value of the two settings will be saved back to the
file if I have changed the values of the settings.

public partial class Form1 : Form
{
XmlDocument doc;
XmlNode latitudenode;
XmlNode longtitudenode;

public Form1()
{
InitializeComponent();
doc = new XmlDocument();
doc.Load("XMLDOMProj.exe.config");
}

private void Form1_Load(object sender, EventArgs e)
{
XmlNodeList nodes;
XmlAttribute attr;
nodes = doc.SelectNodes("//setting");

foreach (XmlNode node in nodes)
{
attr = node.Attributes["name"];
if (attr.InnerText == "latitude")
{
latitudenode = node.ChildNodes[0];
}
else if (attr.InnerText == "longtitude")
{
longtitudenode = node.ChildNodes[0];
}
}
this.textBox1.Text = latitudenode.InnerText;
this.textBox2.Text = longtitudenode.InnerText;
}

private void button1_Click(object sender, EventArgs e)
{
latitudenode.InnerText = textBox1.Text;
longtitudenode.InnerText = textBox2.Text;
doc.Save("XMLDOMProj.exe.config");
}
}

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
J

Jeff Richardson

Thanks Linda,
Using the XML DOM to update the application scoped settings works great!

Now the problem that I am having is reloading the updated settings. After
successfully updating the settings I have attempted to reload the setting
with the following line of code:
Properties.Settings.Default.Reload();

This code executes without any errors, but it does not load the new settings
from the exe.config file. Do you know of way to get then values reloaded?

Thanks
Jeff.
 
L

Linda Liu [MSFT]

Hi Jeff,

Thank you for your response.

The ApplicationSettingsBase.Reload method refreshes the application
settings property values from persistent storage. However, the effect of
calling this method on an application-scoped setting and a user-scoped
setting differs. As for a user-scoped setting, calling this method results
in retrieving the current value from user.config file to the property. And
as for an application-scoped setting, assuming that its value couldn't be
changed at run time, calling this method only results in refreshing the
value of the property with the initial value when the program begin to run.

As for your case, to update the value of the latitude property in memory at
run time, you shoud assign the new value to the property by the statement
"Properties.Settings.Default["latitude"] = 250" after you save the new
value into the exe.config file using XML DOM.

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
J

Jeff Richardson

Thank you Linda,
I figured as much. I now have the solution working correctly with
modifiable application scoped settings as well as user scoped settings!

Maybe this example points out an additional scenario for the Configuration
settings classes of .NET 2.0.

Thanks again for all your help!
Jeff.
 
L

Linda Liu [MSFT]

Hi Jeff,

Thank you for your update and response. You are welcome!

If you have any other questions or concerns, please do not hesitate to
contact us. It is always our pleasure to be of assistance.

Have a nice day!


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
G

Guest

Hello,

I need to be able to do something very similar but am having some problems
with my Config file. My config file looks liek the following -

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="InputDir"
value="C:\Testing\Delapps\ServiceLink\Professional\Current\Import\Excel\Import"/>
<add key="ErrorDir"
value="C:\Testing\Delapps\ServiceLink\Professional\Current\Import\Excel\Error"/>
<add key="BackupDir"
value="C:\Testing\Delapps\ServiceLink\Professional\Current\Import\Excel\Backup"/>
<add key="OutputDir"
value="C:\Testing\Delapps\ServiceLink\Professional\Current\Import\Xml"/>
<add key="Interval" value="3"/>
</appSettings>
</configuration>

I need to update the key values of this config file and am trying to update
the nodes as specified in this thread. I am doing this as follows -

XmlDocument doc = new XmlDocument();

doc.Load("PricingLoader.exe.config");

XmlNodeList nodes;
XmlAttribute attr;

nodes = doc.SelectNodes("appSettings");

foreach (XmlNode node in nodes)
{
attr = node.Attributes["appSettings"];

if (attr.InnerText == "InputDir")
{
inputNode = node.ChildNodes[0];
}

if (attr.InnerText == "OutputDir")
{
outputNode = node.ChildNodes[0];
}

if (attr.InnerText == "BackupDir")
{
backupNode = node.ChildNodes[0];
}

if (attr.InnerText == "ErrorDir")
{
errorNode = node.ChildNodes[0];
}

if (attr.InnerText == "Interval")
{
intervalNode = node.ChildNodes[0];
}
}

inputNode.InnerText = input;
outputNode.InnerText = output;
backupNode.InnerText = backup;
errorNode.InnerText = error;
intervalNode.InnerText = interval;

doc.Save("PricingLoader2.exe.config");

However, this does not update my config file at all. Any ideas??? Thankyou
very much for any help.

David
 

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