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

SebGM2018

New member
Joined
Jun 16, 2018
Messages
4
Programming Experience
3-5
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?
 
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?
 
Yes, It Is

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
 
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.
 
Reply

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?
 
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.
 
Back
Top Bottom