Results 1 to 6 of 6

Thread: Why My List<string> Saved In User Settings doesn´t Save the strings?

  1. #1
    .NET Framework
    .NET 4.5
    Join Date
    Jun 2018
    Posts
    4
    Rep Power
    0

    Why My List<string> Saved In User Settings doesn´t Save the strings?

    Good Day, I Have A List<string> Saved In My App User Settings, this List is supposed to save the file paths of a openfiledialog to then deserialize it, but I already Called the method Save(); And even the method Upgrade(); After adding the strings to the list, but this strings, doesn´t save. Why?

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,865
    Rep Power
    145
    Quote Originally Posted by SebGM2018 View Post
    I Have A List<string> Saved In My App User Settings
    I'm not sure that that's even possible. What was the type of the setting you created? If you think that it's List<string>, can you provide a screenshot of your Settings page to prove it?

    If it's not List<string>, how exactly did you save the data from the List<string> to the setting?

  3. #3
    .NET Framework
    .NET 4.5
    Join Date
    Jun 2018
    Posts
    4
    Rep Power
    0

    Yes, It Is

    Quote Originally Posted by jmcilhinney View Post
    I'm not sure that that's even possible. What was the type of the setting you created? If you think that it's List<string>, can you provide a screenshot of your Settings page to prove it?

    If it's not List<string>, how exactly did you save the data from the List<string> to the setting?
    Yes. It is possible, check the screenshots, to do it, I needed to change the settings.settings xml file, from System.String, to System.Collection.Generic.List.
    Captura de pantalla (27).pngCaptura de pantalla (28).png

  4. #4
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,865
    Rep Power
    145
    Quote Originally Posted by SebGM2018 View Post
    I needed to change the settings.settings xml file, from System.String, to System.Collection.Generic.List.
    That's probably the issue then. Did you not think that there's a reason that you weren't able to do it via the UI? If you want to save a list of Strings then you can use a StringCollection. If you specifically need a List<string> then you can simply copy the data back and forth between the two at startup and shutdown.

  5. #5
    .NET Framework
    .NET 4.5
    Join Date
    Jun 2018
    Posts
    4
    Rep Power
    0

    Reply

    Quote Originally Posted by jmcilhinney View Post
    That's probably the issue then. Did you not think that there's a reason that you weren't able to do it via the UI? If you want to save a list of Strings then you can use a StringCollection. If you specifically need a List<string> then you can simply copy the data back and forth between the two at startup and shutdown.
    Why A String Collection?
    And If I Get The Second Option, How Can I Do It?

  6. #6
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,865
    Rep Power
    145
    Generics were only added to .NET in version 2.0. Prior to that, we generally used an ArrayList for dynamic array functionality. That lacks string typing for the items though, so is not ideal. Because collections of Strings are so common, the System.Collections.Specialized.StringCollection class was included from version 1.0 to provide string typing. As generics seem not to be supported in the Settings subsystem and the StringCollection is explicitly supported, that's what you should use to persist a collection of Strings.

    If you do go with a StringCollection then you can just use it as you would any other collection, calling Add and Remove and the like. If you really need a List<string> then you can do this:
    var myListOfString = new List<string>(Properties.Settings.Default.MyStringCollection);

    or this:
    myListOfString.AddRange(Properties.Settings.Default.MyStringCollection);

    at startup and this:

    Properties.Settings.Default.MyStringCollection.Clear();
    Properties.Settings.Default.MyStringCollection.AddRange(myListOfString);

    at shutdown.

    Note that there is one gotcha when using a StringCollection that catches many people out. When you add a setting of that type, it will be null by default. One way to handle that is to do this at startup:

    if (Properties.Settings.Default.MyStringCollection == null)
    {
    Properties.Settings.Default.MyStringCollection = new StringCollection();
    }

    That will create an object the first time and that object will then be saved at shutdown and loaded on subsequent occasions. A better option is to follow these steps:

    1. Click the Value field for the setting.
    2. Click the (...) button to edit the value.
    3. Add any dummy value.
    4. Click OK.

    Note that an XML snippet is displayed in the Value field and it contains your dummy item.

    5. Click the (...) button again.
    6. Delete the dummy value.
    7. Click OK.

    Note that the XML snippet remains but your dummy item is gone. The setting will now contain an empty StringCollection object the first time the app runs.

Similar Threads

  1. Question save xml string passed as string to webservice method in a file
    By RadhikaChandra in forum C# General Discussion
    Replies: 7
    Last Post: 04-26-2018, 2:34 AM
  2. Replies: 1
    Last Post: 09-14-2016, 10:32 PM
  3. Replies: 0
    Last Post: 06-21-2016, 9:47 AM
  4. Question Unhandled exception string problem user input
    By sandcounter in forum VS.NET General Discussion
    Replies: 3
    Last Post: 10-19-2015, 11:03 PM
  5. Remove Item From List<string>
    By inkedGFX in forum C# General Discussion
    Replies: 1
    Last Post: 03-02-2013, 8:01 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •