How to store a list of objects in application settings

前端 未结 5 1226
时光取名叫无心
时光取名叫无心 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 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(json);
    

提交回复
热议问题