How to save a List<string> on Settings.Default?

C#.NetWinforms

C# Problem Overview


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# Solutions


Solution 1 - C#

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

Solution 2 - C#

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();

Solution 3 - C#

I know this is an older question, but I feel it is noteworthy to add that you can create a custom class that inherits whatever kind of list you want to use and it makes it very simple to set. For example, if you want a List<CustomData>, you could create the class like this:

using System.Collections.Generic;

namespace YourNamespace
{
    public class CustomCollection : List<CustomData>
    {
    }
}

then open the settings like @pr0gg3r suggested and add this in the <Settings> section:

    <Setting Name="SomeSetting" Type="YourNamespace.CustomCollection" Scope="User">
      <Value Profile="(Default)" />
    </Setting>

You won't be able to use the settings designer, unless it's a basic data type like string or int. If you are using a custom data type, you'll have to initialize it on startup before using it, but I find it still looks more elegant than trying to directly use a list in the settings.

Solution 4 - C#

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;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrunoLMView Question on Stackoverflow
Solution 1 - C#pr0gg3rView Answer on Stackoverflow
Solution 2 - C#BrunoLMView Answer on Stackoverflow
Solution 3 - C#TimberwolfView Answer on Stackoverflow
Solution 4 - C#Joshua LumleyView Answer on Stackoverflow