473,782 Members | 2,479 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.Sett ings///////
//get the key from Properties.Sett ings
string myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
//This line seems to only set the value in memory and not in the file.
Properties.Sett ings.Default["myKey"] = "myNewValue ";
//myPropertiesSet ting will now be equal to "myNewValue "
myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
//things look good, except when I restart the application the new value
//was not stored in the settings.cs file.
//////Using ConfigurationMa nager.AppSettin gs///////
NameValueCollec tion appSettings = ConfigurationMa nager.AppSettin gs;
//get the key from ConfigurationMa nager.AppSettin gs
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
Sep 30 '08 #1
3 9747
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 AppConfiguratio n 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 AppConfiguratio n. I make AppConfiguratio n a singleton and
have it load and save itself automatically with the XmlSerializer, you can
even precompile the XmlSerializatio n 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" wrote:
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.Sett ings///////
//get the key from Properties.Sett ings
string myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
//This line seems to only set the value in memory and not in the file.
Properties.Sett ings.Default["myKey"] = "myNewValue ";
//myPropertiesSet ting will now be equal to "myNewValue "
myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
//things look good, except when I restart the application the new value
//was not stored in the settings.cs file.
//////Using ConfigurationMa nager.AppSettin gs///////
NameValueCollec tion appSettings = ConfigurationMa nager.AppSettin gs;
//get the key from ConfigurationMa nager.AppSettin gs
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

Sep 30 '08 #2
On Sep 29, 10:13*pm, Jon <J...@discussio ns.microsoft.co mwrote:
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.Sett ings///////
//get the key from Properties.Sett ings
string myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
//This line seems to only set the value in memory and not in the file.
Properties.Sett ings.Default["myKey"] = "myNewValue ";
//myPropertiesSet ting will now be equal to "myNewValue "
myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
//things look good, except when I restart the application the new value
//was not stored in the settings.cs file.

//////Using ConfigurationMa nager.AppSettin gs///////
NameValueCollec tion appSettings = ConfigurationMa nager.AppSettin gs;
//get the key from ConfigurationMa nager.AppSettin gs
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...iguration.aspx

Sep 30 '08 #3


"Ciaran O''Donnell" wrote:
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 AppConfiguratio n 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 AppConfiguratio n. I make AppConfiguratio n a singleton and
have it load and save itself automatically with the XmlSerializer, you can
even precompile the XmlSerializatio n 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" wrote:
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.Sett ings///////
//get the key from Properties.Sett ings
string myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
//This line seems to only set the value in memory and not in the file.
Properties.Sett ings.Default["myKey"] = "myNewValue ";
//myPropertiesSet ting will now be equal to "myNewValue "
myPropertiesSet ting = (string)Propert ies.Settings.De fault["myKey"];
//things look good, except when I restart the application the new value
//was not stored in the settings.cs file.
//////Using ConfigurationMa nager.AppSettin gs///////
NameValueCollec tion appSettings = ConfigurationMa nager.AppSettin gs;
//get the key from ConfigurationMa nager.AppSettin gs
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 ConfigurationMa nager 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 =
ConfigurationMa nager.OpenExeCo nfiguration(Con figurationUserL evel.None);

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

myConfigObject. AppSettings.Set tings.Remove("r ootDrive");

myConfigObject. AppSettings.Set tings.Add(new
KeyValueConfigu rationElement(" rootDrive", "J:\\"));

myConfigObject. Save();
Sep 30 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1746
by: Larry Woods | last post by:
I have created a table that I want to fill with calculated values. I have given ID's to each cell. This must NOT be the way since I can't address these ID's. How do I store values in table cells from my PostBack? Thanks. Larry Woods
1
529
by: Kevin Lewis | last post by:
Is there .NET framework code that I can call from an assembly (.dll), that will load the settings of a configuration file into the .NET framework. For example you can set up Switches and TraceListeners through config settings such as: <system.diagnostics> <switches> <add name="MySwitch" value="31" /> </switches> <trace>
2
1542
by: Mark Friedman | last post by:
I've seen lots of documentation on how to create and read custom configuration settings but I'm interested in reading a standard setting (i.e. the loginUrl attribute of the authentication/forms setting). Do I have to resort to using straight XML reading or is there some other way to access standard config settings? Thanks in advance. -Mark
2
2523
by: Max | last post by:
This is the best method I've found to store and load settings from database. I load the settings once at start as public variables. The method can be called easily anytime you want to load the new settings from database. Comments very welcome... I'd like to refine this... besides the error handling I need to add for the db conn... Global.asax --------------- Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) Dim...
3
1543
by: Augustin Prasanna. J | last post by:
Hi How do i access the settings defined in the web.config during runtime. GetConfig method is used to access the user defined sections. i would like to get the customErrors mode defined in the web.config from a code behind page. how do i achieve this Thanks Augustin
1
1236
by: Abraham Andres Luna | last post by:
hello everyone, i set up a folder to have integrated windows authentication through iis. in order for this to work with asp.net, i have to set the identity.impersonate=true in the web.config for that folder. my questions is, why doesn't the subfolders inherit the impersonate setting. when i try to access a page in a subfolder i am prompted for login info. i guess the answer would be to have a web.config in every subfolder, but i
3
24546
by: davepkz | last post by:
I have a basic question about managing user.config settings: In my C# app, I save the user's preferences to the config file. But each time I release a new build of the app, something in .NET creates a new folder (named for the build number) in Documents And Settings. The result is the users lose all their settings and have to start over. What is the standard way to allow users to maintain their settings when they get a new version of...
0
2443
by: CSharpner | last post by:
How can I do the following?: Have a label on a form have its Text property bound to a setting from one of my DLL library's config files? I've followed these instructions to successfully get the DLL settings into the web.config: http://groups.google.com/group/microsoft.public.dotnet.languages.csharp/browse_frm/thread/173c2b36f3c679a4/e34b1675db9b4486?lnk=st&q=dotnet+applicationSettings+web.config+dll+properties&rnum=1#e34b1675db9b4486
0
855
by: klarry79 | last post by:
hi there I am using Windows Sharepoint Services 2.0. and SQL Server 2005. I have a web part that pulls its content from the SQL Server. I get the following error when i try to load the web part on my SharePoint site "System error: Unable to Load Search Config Settings - reason: Operation Timed Out" Any assist on this
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10080
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.