How to save a List on Settings.Default?

2019-01-05 00:42发布

I have a ListBox on my Form, I want to save it and load the values when I start the application again.

How can I save a list on PrjName.Properties.Settings.Default?

标签: c# .net winforms
3条回答
一纸荒年 Trace。
2楼-- · 2019-01-05 01:19

When using the natively supported Type System.Collections.Specialized.StringCollection

I used this code:

        System.Collections.Specialized.StringCollection SavedSearchTerms = new System.Collections.Specialized.StringCollection();

        if (Properties.Settings.Default.SavedSearches != null)
        {
            SavedSearchTerms = Properties.Settings.Default.SavedSearches;
        }

        SavedSearchTerms.Add("Any Value");

        Properties.Settings.Default.SavedSearches = SavedSearchTerms;
查看更多
戒情不戒烟
3楼-- · 2019-01-05 01:36

No problem at all! Create a new setting, e.g. "MyListOfStrings", type doesn't matter.

enter image description here

then open settings file in a xml editor

enter image description here enter image description here

your file will look like this:

enter image description here

now change it as shown below and save it

enter image description here

well, that's all, now it will look like that:

enter image description here

and in code:

enter image description here

查看更多
祖国的老花朵
4楼-- · 2019-01-05 01:40

I found out that I can't directly save a List<string> on the application settings, but I saw that I can save a StringCollection.

And here I found out that it's very simple to convert from a StringCollection to a List<string>

var list = stringCollection.Cast<string>().ToList();
查看更多
登录 后发表回答