I would like to use a StringCollection as application settings, however while reading it's not a problem, I found out that settings are not stored.
How to make it works? Any workaround? What's the problem here?
Code I'm using:
private static void AddToRecentProfiles(string path)
{
if (SpellCaster3.Properties.Settings.Default.RecentProfiles == null)
SpellCaster3.Properties.Settings.Default.RecentProfiles = new StringCollection();
int index = SpellCaster3.Properties.Settings.Default.RecentProfiles.IndexOf(path);
if (index >= 0)
SpellCaster3.Properties.Settings.Default.RecentProfiles.Swap(index, 0);
else
SpellCaster3.Properties.Settings.Default.RecentProfiles.Insert(0, path);
if (SpellCaster3.Properties.Settings.Default.RecentProfiles.Count > SpellCaster3.Properties.Settings.Default.MaxRecentProfiles)
SpellCaster3.Properties.Settings.Default.RecentProfiles.RemoveAt(SpellCaster3.Properties.Settings.Default.RecentProfiles.Count - 1);
SpellCaster3.Properties.Settings.Default.Save();
OnRecentProfilesChanged(SpellCaster3.Properties.Settings.Default.RecentProfiles, EventArgs.Empty);
}
I found the solution by myself, the problem is that if you create your StringCollection with "new" keyword and save settings, they don't get stored.
The way to fix this is to "force" the application settings designer to create it for you, how to do it? Well it's quite easy, put stringcollection as type and insert 2/3 strings. Press ok. Then edit again this value and remove all strings to leave it "created but empty".
After this, you can just use it by adding/removing strings and save settings. And you will be sure it won't be null!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With