views:

461

answers:

2

I'm not necessarily referring to app.configs, but a custom configuration file that would store my program's state whenever a user hits a "save" button.

In my example, it's a UI builder that allows the user to choose which "fields" are to be displayed on the left and right columns in a two-column screen, along with the order of the fields in each column. There's other stuff too, but you get the idea.

The user could open any configuration file they've saved and populate the UI builder appropriately.

I'm thinking this could be stored in an XML file, but I was wondering what sort of suggestions people would have on how to write it out, how to read from it (use LINQ maybe?) and other general approaches, including versioning etc. I'm sure this has come up before and I'd like to hear about best practices. Thanks!

+5  A: 

Does it have to be somewhat human readable? Or does the user not care... just save my settings... and let me reload them later?

If not, just create/design a bunch of objects with meaningful properties... create instances... and serialize (binary or soap or custom) the object to disk. Then, you can always "re-hydrate" your object later from disk.

When possible, I always like to not reinvent the wheel. Let the framework do the work.

Kris Krause
+3  A: 

The method I use for my program is to keep my configuration values in a class that uses a DataTable in a DataSet to store values. When I want to save it, I use DataSet.WriteXML() to whatever filename the user wants.

My current use is pretty simple (one DataTable with key, value columns) but it easily expands to multiple related DataTables with specific columns for various information. The DataSet handles the file io.

Then it's a breeze to read back in with DataSet.ReadXML().

An advantage of this method over serializing classes is that it is much easier to handle files saved from a previous version of your program. You can set a value in the table for the version of the program used to save it, so your newer version will know the older file won't have any values added in the newer version - and it can set them appropriately to update the file version.

If you add new features to a class (values, methods) it's serialized file will be different than earlier versions - not sure how easy it would be to handle those older files.

Here is a simple example class.

Used like this, makes a file like this:

To create a new one:
      Dim UIcfg As UIsettings = New UIsettings("TestSettings.cfg")

      UIcfg.setGeneralValue("Version", "1.0.0")
      UIcfg.setGeneralValue("Author", "Bobs YourUncle")

      UIcfg.setFieldValues("FirstName", "Left", "1")
      UIcfg.setFieldValues("LastName", "Right", "1")
      UIcfg.setFieldValues("ShoeSize", "Left", "2")
      UIcfg.setFieldValues("ShoeColor", "Left", "3")

      UIcfg.Save()

To get values from it:
      Dim value As String = ""
      Dim values As String = ""
      value = UIcfg.getGeneralValue("Author")
      values = UIcfg.getFieldValues("FirstName")

Code is so much easier than trying to explain a concept. :-)

Ron

Ron Savage
Sounds like a good tactic. Thanks!
larryq