How to store values in app.config or settings.cs

J

Jon

Hello,
I have tried to use the app.config and settings.cs files to store my data
(which I want to be user changeable at runtime). I can write to (what I
assume is an object in memory) and it does seem to work...however, once the
application is closed and reopened the changes are lost. How do I persist
the information?


Here is some sample code using both methods:
//////Using Properties.Settings///////
//get the key from Properties.Settings
string myPropertiesSetting = (string)Properties.Settings.Default["myKey"];
//This line seems to only set the value in memory and not in the file.
Properties.Settings.Default["myKey"] = "myNewValue";
//myPropertiesSetting will now be equal to "myNewValue"
myPropertiesSetting = (string)Properties.Settings.Default["myKey"];
//things look good, except when I restart the application the new value
//was not stored in the settings.cs file.


//////Using ConfigurationManager.AppSettings///////
NameValueCollection appSettings = ConfigurationManager.AppSettings;
//get the key from ConfigurationManager.AppSettings
String myAppSetting = appSettings["myKey"];
//This line seems to only set the value in memory and not in the file.
appSettings["myKey"] = "myNewValue";
//appSettings will now be equal to "myNewValue"
myAppSetting = appSettings["myKey"];
//things look good, except when I restart the application the new value
//was not stored in the app.config file.
Thanks,
Jon
 
C

Ciaran O''Donnell

I know this might not be the recommended way of doing it, but I normally
create a a project for configuration objects which I mark as Serializable. I
then serialize the objects out of and into xml in a file in the same folder
as my app.
I make a class like AppConfiguration which has app settings. Then I make
other classes for specific settings for specific parts of the app and add a
property for it to AppConfiguration. I make AppConfiguration a singleton and
have it load and save itself automatically with the XmlSerializer, you can
even precompile the XmlSerialization assemblies for it.
This is a basic description, let me know if you like the general idea and
I'll slap a demo of it up on my blog.
 
G

G.S.

Hello,
I have tried to use the app.config and settings.cs files to store my data
(which I want to be user changeable at runtime).  I can write to (what I
assume is an object in memory) and it does seem to work...however, once the
application is closed and reopened the changes are lost.  How do I persist
the information?

Here is some sample code using both methods:
//////Using Properties.Settings///////
//get the key from Properties.Settings
string myPropertiesSetting = (string)Properties.Settings.Default["myKey"];
//This line seems to only set the value in memory and not in the file.
Properties.Settings.Default["myKey"] = "myNewValue";
//myPropertiesSetting will now be equal to "myNewValue"
myPropertiesSetting = (string)Properties.Settings.Default["myKey"];
//things look good, except when I restart the application the new value
//was not stored in the settings.cs file.

//////Using ConfigurationManager.AppSettings///////
NameValueCollection appSettings = ConfigurationManager.AppSettings;
//get the key from ConfigurationManager.AppSettings
String myAppSetting = appSettings["myKey"];
//This line seems to only set the value in memory and not in the file.
appSettings["myKey"] = "myNewValue";
//appSettings will now be equal to "myNewValue"
myAppSetting = appSettings["myKey"];
//things look good, except when I restart the application the new value
//was not stored in the app.config file.
Thanks,
Jon

I have found this article (and its links) to provide helpful
information in that direction
http://www.codeproject.com/KB/dotnet/mysteriesofconfiguration.aspx
 
J

Jon

Ciaran O''Donnell said:
I know this might not be the recommended way of doing it, but I normally
create a a project for configuration objects which I mark as Serializable. I
then serialize the objects out of and into xml in a file in the same folder
as my app.
I make a class like AppConfiguration which has app settings. Then I make
other classes for specific settings for specific parts of the app and add a
property for it to AppConfiguration. I make AppConfiguration a singleton and
have it load and save itself automatically with the XmlSerializer, you can
even precompile the XmlSerialization assemblies for it.
This is a basic description, let me know if you like the general idea and
I'll slap a demo of it up on my blog.


--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com


Jon said:
Hello,
I have tried to use the app.config and settings.cs files to store my data
(which I want to be user changeable at runtime). I can write to (what I
assume is an object in memory) and it does seem to work...however, once the
application is closed and reopened the changes are lost. How do I persist
the information?


Here is some sample code using both methods:
//////Using Properties.Settings///////
//get the key from Properties.Settings
string myPropertiesSetting = (string)Properties.Settings.Default["myKey"];
//This line seems to only set the value in memory and not in the file.
Properties.Settings.Default["myKey"] = "myNewValue";
//myPropertiesSetting will now be equal to "myNewValue"
myPropertiesSetting = (string)Properties.Settings.Default["myKey"];
//things look good, except when I restart the application the new value
//was not stored in the settings.cs file.


//////Using ConfigurationManager.AppSettings///////
NameValueCollection appSettings = ConfigurationManager.AppSettings;
//get the key from ConfigurationManager.AppSettings
String myAppSetting = appSettings["myKey"];
//This line seems to only set the value in memory and not in the file.
appSettings["myKey"] = "myNewValue";
//appSettings will now be equal to "myNewValue"
myAppSetting = appSettings["myKey"];
//things look good, except when I restart the application the new value
//was not stored in the app.config file.
Thanks,
Jon
Thanks Ciaran,
I would love an example of what you are talking about if you have time
to provide it. But, I really was wanting to use the built in C# framework
for this because it can do a ton of work I unless it doesn't do what I need,
I don't want to have to roll my own code. (although I would love to know how
just in case I need to : )
BTW, I think I did figure out how to use the ConfigurationManager class
to save to the file although I wish there was a replace() instead of just a
remove() and an add()..here is the code I came up with, let me know if you
have any better suggestions : ) Thanks again.
Jon

Configuration myConfigObject =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

//try to read the value...should be null the first time and then have a
value after that
KeyValueConfigurationElement rootKVE =
myConfigObject.AppSettings.Settings["rootDrive"];

myConfigObject.AppSettings.Settings.Remove("rootDrive");

myConfigObject.AppSettings.Settings.Add(new
KeyValueConfigurationElement("rootDrive", "J:\\"));

myConfigObject.Save();
 

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