Tags: applicationsettingsbase, bit, configurationmanager, local, method, microsoft, msdn, net, newbie, persist, persistence, programatically, software, versus, visual

ApplicationSettingsBase Versus ConfigurationManager

On Microsoft » Microsoft Visual C#

6,024 words with 1 Comments; publish: Thu, 27 Dec 2007 05:18:00 GMT; (30093.75, « »)

Hi, I am a bit of newbie when it comes to the best method to use for local data persistence in .NET 2.0.

I am trying to programatically persist some slightly complex objects (eg. Dictionary<string, object>) for user settings in my app and am not sure which is the best mechanism to do this in .NET 2.0. I've managed to create a bunch of settings classes derived from ApplicationSettingsBase that work flawlessly when serializing as binary, but only seems to be able to xml serialize the simplest of objects (eg. List<SimpleObject>). I understand that Dictionaries cannot be xml serialized, but even a list of objects storing key/value pairs does not work once your object reaches a certain level of complexity.

So then I started looking at classes such as ConfigurationManager, ConfigurationElement and ConfigurationElementCollection, which seem to be perfect for persisting complex lists of objects as well as having lots of useful options such as encrypting properties. The problem using these classes is that they do not seem to be designed for writing to the same user.config file used by ApplicationSettingsBase derived classes (as they always seem to have the file locked). Although ApplicationSettingsBase derived classes leave the file locked while the app is running, different derived classes can all access it fine (I'm assuming the base manages all this).

I would really appreciate it if someone could tell me if there is any way to get around the problems of using serialize as xml with ApplicationSettingsBase and also which situations are best to use ApplicationSettingsBase and which are best for ConfigurationManager.

Thanks in advance


All Comments

Leave a comment...

    • If one wants to use the Settings method to serialize a dictionary with objects, one needs to think out of the box. Here is one thought:

      Override ToString of the object(s) classes being serialized to return a snapshot of the object seperated by a special character:

      public class GenObject


      public GenObject(int item)


      _item = item;


      private int _item;

      public override string ToString()

      { // Class Name | item value

      return string.Format("GenObject|{0}", _item.ToString());



      One can work through the hash table in a foreach via looking at the key/value pairs and join them into one long string thanks to the override of the ToString on each of the objects. Here is a generated example but it shows proof of concept:

      public static List<string> SerializeOut()


      List<string> outputSerialized = new List<string>();

      Dictionary<string, object> genList = new Dictionary<string, object>();

      genList.Add("ABC", new GenObject(1));

      genList.Add("DEF", new GenObject(2));

      genList.Add("GHI", new GenObject(3));

      foreach (KeyValuePair<string, object> item in genList)

      outputSerialized.Add(string.Format("{0}|{1}", item.Key, item.Value.ToString()));

      return outputSerialized;


      That list can be saved using the Settings Methodology you mentioned. Once the application is restarted Simply read the items and recreate the objects appropriately by unwinding the strings.

      public static void SerializeIn(List<string> items)


      foreach(string HashItem in items)


      string[] tokens = HashItem.Split('|');

      Console.WriteLine(string.Format("Key {0} Class {1} item {2}", tokens[0], tokens[1], tokens[2]));



      Final output looks like this

      Key ABC Class GenObject item 1
      Key DEF Class GenObject item 2
      Key GHI Class GenObject item 3

      #1; Tue, 04 Sep 2007 20:20:00 GMT