tags:

views:

362

answers:

7

Ok, this is a total newb question, so please forgive me.

What is the best way to store variables so that they persist and are recoverable? I have a small application that uses about 10 variables (string and decimal) as settings. Currently, I convert them all to strings, if needed, put them into an array and serialize the array to a file.

This is a pain if I ever want to add a variable. So, I was thinking about using a hashtable and serializing that. Again, not sure what is the best way to do this.

Some requirements I have are that the data needs to be stored securely (encrypted), and must be accessible by other applications (I have two other small apps that read the settings).

I know I am over-complicating something so basic and simple. This must be done in nearly every application built.

TIA

+1  A: 

why don't you just put them in the app.config? (or web.config)

jaltiere
sorry, didn't read the requirements fully. If I have settings that I need to persist across applications I generally use a database.
jaltiere
What kind of DB? Sqlite?
robotshapes
I guess that would depend on what was available, any database would do. SQLLite, SQL Server, Oracle, MySQL....heck you could even use Raven
jaltiere
No database, and if you do, use SQL Server Compact Edition (http://www.microsoft.com/Sqlserver/2005/en/us/compact.aspx), but you can just manually create a DataTable and serialize/deserialize (ReadXML/WriteXML). Don't make this harder than it is.
AMissico
+3  A: 

Use an application/web config file, and use the ConfigurationManager.AppSettings[configurationItemName] method. (in System.Configuration)

DaveRead
you can share the settings too:http://stackoverflow.com/questions/426245/how-to-share-app-config
Tim Schmelter
You can also use the Configuration Application Block in Enterprise Library to load/save settings to .config files. (including custom config sections that can be XML serialized)
DaveRead
It is a "newbie" question. Taking that fact into consideration, Enterprise Library is overkill.
AMissico
A: 

If I used a database anyway, I'd probably add a table in there and store them, otherwise I think I'd go with an XML file of some sort, I'd be relucant to add the extra complexity of a DB for just a few settings.
With either of those solution you could easily add more settings (older applications would just ignore them) as well as specify the datatype (as an attribute in the XML).

ho1
A: 

You can store the values in the system registry:

Getting a Key:

// This will create the key if it does not exist.
String keyPath = "Sofware\Foo\Bar";  // use "Software\Company\App" or similar
RegistryKey appKey = Registry.CurrentUser.CreateSubKey(keyPath);

Saving a value:

String config = "SomeConfigurationValue";
appKey.SetValue("ConfigString", config);

Loading a value:

String config = (string)appKey.GetValue("ConfigString", "Default Value");
mbmcavoy
A: 

Just to make sure I understand

Requirements: 1. Store user specific settings for your application in a file or in a database 2. From time to time, you may add additional properties.

Is this web based? win forms?, wcf?

My initial thought is to include a version number when persisting.

Each time you update the settings class, create a new settings class that inherits from the last version. Add your new properties and update the version number.

Storage: In a DB, two fields: 1- version 2- serialized settings data. In a file, two entries 1- version 2- serialized settings data.

When serializing, make sure to include the settings class version number.

When deserializing, use a Factory to retrieve the correct version of the settings class.

One thing to keep in mind is that you would have to account for older settings instances in your application.

If you add a "Background Color" property today, none of the settings files from before today will have access to it. You'll need to ensure the application can handle that situation.

So if the Factory finds an older version of the settings class, it can use the saved data to create the newest version of the class and fill the new properties with default values.

Good Luck,

Patrick

Patrick
+3  A: 

The simpliest, effective and most flexible approach is to create a class, add settings, then serialize/deserialize when needed. This source code for the class can be reused in other assemblies, and persistence can be anywhere. Make sure this class knows how to serialize/deserialize itself because of your security requirement. This ensures the implementation stays with the class. Then the calling assembly just needs to create the object by calling a static/shared method.

This gives you strongly-typed settings, versioning, ability to add new settings, and even complex data types (other classes). This object can even be passed to other objects as arguments, and since it supports serialization, it is very flexible.

Example

See How-To (Object Class => Binary Serialization => To Memory => Encrypt => Save to File) at http://social.msdn.microsoft.com/forums/en-US/netfxremoting/thread/68c200c2-4aa4-48dc-95be-6fe077fd10f4/

Reference

AMissico
+1 - I've used this technique before, and it works out well. A plus is that if you use a separate file instead of the (app|web).config, the application will not need to be recycled. And you avoid having to deal with keys and values.
Grant Palin
A: 

Store the settings to classes, and serialize these objects to Isolated Storage. Make sure to set the isolation level to the user level, so that other apps can read the settings for the current user.

http://msdn.microsoft.com/en-us/library/8dzkff1s%28VS.80%29.aspx

IsolatedStorageFile, which derives from IsolatedStorage, provides basic management of stored assembly and application files. An instance of the IsolatedStorageFile class represents a single store located in the file system.

code4life