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'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?
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.
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.
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.
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);
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.