0

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.

Nitro.de
  • 821
  • 10
  • 27
  • 2
    The Save() method only saves anything when it knows that a setting has changed. It doesn't know that you called Add(), that did not modify the property. It only modified the list. You'll have to reassign CustomTabs to make it smarter. – Hans Passant Feb 04 '16 at 14:32

1 Answers1

0

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>;
}
bkdev
  • 432
  • 4
  • 9
  • I had to edit it manually because the Gui wont get access to List if you dont do it. I try to restore it, but its empty as i said. Btw all the simple types e.g. string get saved – Nitro.de Feb 04 '16 at 13:44
  • well, you have manually edited both settings.settings file and settings.designer.cs file. Can you throw away your edit on settings.settings file but still keep your edit on settings.designer.cs file and give it a try ? – bkdev Feb 04 '16 at 13:47
  • Makes no difference. – Nitro.de Feb 04 '16 at 13:51
  • @Nitro.de I added some sample code that works for me. sorry if it couldn't help you further – bkdev Feb 04 '16 at 13:59
  • Well i tried your snipped and it works, maybe the class i try to insert into the list is just to huge or complex. – Nitro.de Feb 04 '16 at 14:21
  • ah! may be your custom class should handle serialization ? please see http://stackoverflow.com/questions/8688724/how-to-store-a-list-of-objects-in-application-settings – bkdev Feb 04 '16 at 14:33