How to store a list of objects in application settings

前端 未结 5 1197
时光取名叫无心
时光取名叫无心 2020-12-02 21:43

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!

相关标签:
5条回答
  • 2020-12-02 21:48

    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.

    0 讨论(0)
  • 2020-12-02 22:01

    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.

    0 讨论(0)
  • 2020-12-02 22:02

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

    • Xml Serialization
    • Binary serialization with proto-buf
    • Json serialization
    • ORM mapping to some embedde database Sqlite

    and many others...

    0 讨论(0)
  • 2020-12-02 22:10

    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);
    
    0 讨论(0)
  • 2020-12-02 22:11

    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.

    0 讨论(0)
提交回复
热议问题