19

I have recently became familiar with C# application settings, and it seems cool.
I was searching for a way to store a list of custom objects, but I couldn't find a way!
Actually I saw a post to store int[], but it wasn't helpful for this problem.
I tried to change the config of that solution in order to make it suitable for my problem. the XML config file of that was:

<Setting Name="SomeTestSetting" Type="System.Int32[]" Scope="User">
  <Value Profile="(Default)" />
</Setting>

I tried to address my object as quoted below in the type attribute but it wasn't helpful since it doesn't recognizing my object... I tried "type = List" and "type="tuple[]"
both of these options didn't help me!

I have a class looks like:

class tuple
    {
        public tuple()
        {
            this.font = new Font ("Microsoft Sans Serif",8);
            this.backgroundcolor_color = Color.White;
            this.foregroundcolor_color = Color.Black;
        }
        public string log { get; set; }
        public Font font { get ; set; }
        public String fontName { get; set; }
        public string foregroundcolor { get; set; }
        public Color foregroundcolor_color { get; set; }
        public string backgroundcolor { get; set; }
        public Color backgroundcolor_color { get; set; }
        public Boolean notification { get; set; }
    }

and I want to store a list in application setting.
So is there any way to achieve this purpose.
Thanks in advance.
Cheers,

Community
  • 1
  • 1
Ehsan
  • 4,334
  • 7
  • 39
  • 59
  • Possible duplicate of http://stackoverflow.com/questions/8627887/store-a-range-of-values-in-web-config-what-data-structure-to-use/8627974#8627974 – Ivo Dec 31 '11 at 15:03
  • take a look at here http://stackoverflow.com/questions/922047/store-dictionarystring-string-in-application-settings it might be helps you... – Glory Raj Dec 31 '11 at 15:06

5 Answers5

40

You can use BinaryFormatter to serialize list of tuples as byte array and Base64 (as quite efficient way) to store byte array as string.

First of all change your class to something like that (hint: [SerializableAttribute]):

[Serializable()]
public class tuple
{
    public tuple()
    {
        this.font = new Font("Microsoft Sans Serif", 8);
    //....
}

Add property in settings named tuples and type of string.

tuples in Settings

Then you can use two methods to load and save generic list of tuples (List<tuple>):

void SaveTuples(List<tuple> tuples)
{
    using (MemoryStream ms = new MemoryStream())
    {
        BinaryFormatter bf = new BinaryFormatter();
        bf.Serialize(ms, tuples);
        ms.Position = 0;
        byte[] buffer = new byte[(int)ms.Length];
        ms.Read(buffer, 0, buffer.Length);
        Properties.Settings.Default.tuples = Convert.ToBase64String(buffer);
        Properties.Settings.Default.Save();
    }
}

List<tuple> LoadTuples()
{
    using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(Properties.Settings.Default.tuples)))
    {
        BinaryFormatter bf = new BinaryFormatter();
        return (List<tuple>)bf.Deserialize(ms);
    }
}

Example:

List<tuple> list = new List<tuple>();
list.Add(new tuple());
list.Add(new tuple());
list.Add(new tuple());
list.Add(new tuple());
list.Add(new tuple());

// save list
SaveTuples(list);

// load list
list = LoadTuples();

I leave null, empty string and exception checking up to you.

Andrew Grinder
  • 585
  • 5
  • 21
Michał Powaga
  • 22,561
  • 8
  • 51
  • 62
3

Application configuration is not the good choice for storing the data at application runtime. For this use any available in .NET serialization option like

and many others...

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

As Mr.Tigran mentioned You can simply use Newtonsoft.Json Nuget Package to convert your Object(which can be a List or contain a List) using serialization and save it as a string (I made a string variable in Settings with "User Scope" and named it "myString"):

string json = JsonConvert.SerializeObject(myObject);
Properties.Settings.Default.myString = json;
Properties.Settings.Default.Save();

To load it we use deserialization :

string json = Properties.Settings.Default.myString;
myObject myobject = JsonConvert.DeserializeObject<myObject>(json);
Aria
  • 1
0

I'm not sure what you're wanting to do is best done in application settings. What you might want to look into is XDocument, and storing the values you need in a seperate configuration file.

tbddeveloper
  • 2,407
  • 1
  • 23
  • 39
  • Thanks for your time and consideration could you pleases give me some hints about XDocument and what it is? and what did you mean by storing the values in a seperate config file? – Ehsan Dec 31 '11 at 14:54
  • XDocument is one of the classes for manipulating xml files within .NET. http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument.aspx If you're actually looking to store the entire stucture, XML serialization might be your better bet though. – tbddeveloper Dec 31 '11 at 15:35
0

You can write custom types to extend .config files. But this will not be storing your own arbitrary types in an existing section of confirmation but adding custom sections.

A custom configuration type could, by providing completely custom logic for child nodes, hold XML serialised data. I would argue that this is abusing the configuration system: it is for storing settings not complete state.

If this is what you want, there is a simple example in the documentation for ConfigurationSection.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • Thanks for you response, but do you have any suggestion for achieving this purpose? to save this object and retrieve it later? – Ehsan Dec 31 '11 at 15:00
  • @Ehsan Define a custom class based on `ConfigurationSection` with the right set of properties. Use `ConfigurationManager` to load config (including user levels) and, later, save. – Richard Jan 01 '12 at 08:01