Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StringCollection in application settings doesn't get stored

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);
    }
like image 914
Francesco Belladonna Avatar asked Dec 06 '22 20:12

Francesco Belladonna


1 Answers

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!

like image 198
Francesco Belladonna Avatar answered Jan 11 '23 22:01

Francesco Belladonna