List<myClass> not saving in settings.settings

2k views Asked by At

I'm trying to save a List<T> into the settingsfile of my project.

I've edited the settings.settings file and added

<Setting Name="CustomTabs" Type="System.Collections.Generic.List&lt;CustomTabItem&gt;" Scope="User">
      <Value Profile="(Default)" />
</Setting>

and edited the settings.designer.cs too

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public global::System.Collections.Generic.List<CustomTabItem> CustomTabs {
    get {
        return ((global::System.Collections.Generic.List<CustomTabItem>)(this["CustomTabs"]));
    }
    set {
        this["CustomTabs"] = value;
    }
}

So the Listtype is available in the settings-gui.

Now if I make Properties.Settings.Default.CustomTabs.Add(tab); The list gets filled, however if I call Save(); and restart the App, the list ist empty again.

Am I missing something to make it work? I'm using Visual Studio 2015.

1

There are 1 answers

6
bkdev On

You don't have to change the settings.settings file

Try removing the setting

<Setting Name="CustomTabs" Type="System.Collections.Generic.List&lt;CustomTabItem&gt;" Scope="User">
      <Value Profile="(Default)" />
</Setting>

from the settings.settings file

Also, remember to read back the stored "CustomTabs" settings value during application startup.

EDIT:2 No harm even if settings.settings file is changed like below:

<Setting Name="myTestDataList" Type="System.Collections.Generic.List&lt;TestData&gt;" Scope="User">
      <Value Profile="(Default)" />
    </Setting>

The sample application is still able to retrieve the previous data during next application start up.

EDIT: I made a sample application:

Added below setting manually in settings.Designer.cs

[global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public System.Collections.Generic.List<TestData> myTestDataList
        {
            get
            {
                return ((System.Collections.Generic.List<TestData>)(this["myTestDataList"]));
            }
            set
            {
                this["myTestDataList"] = value;
            }
        }

My data class

public class TestData
    {
        public string A {get; set;}
        public string B { get; set; }
    }

button-1: stores the data

private void button1_Click(object sender, EventArgs e)
        {
            List<TestData> myList = new List<TestData>();
            myList.Add(new TestData() { A = "Str1", B = "Str2" });
            myList.Add(new TestData() { A = "Str3", B = "Str4" });

            Properties.Settings.Default["myTestDataList"] = myList;
            Properties.Settings.Default.Save();
        }

button-2: retrieves the data even during next application run

private void button2_Click(object sender, EventArgs e)
{
    List<TestData> myList = Properties.Settings.Default["myTestDataList"] as List<TestData>;
}