开发者

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);
    }


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!


Application settings can be scoped at the application level and at the user level and you can only write to settings at the user level, so if you have a StringCollection scoped at the application level you can only read the values that you defined at compile time and adding to the collection at runtime will have no effect the next time you start your application.

You can scope it at the user level if you want changes to propagate between application runs.


Another reason this could happen is if one thinks it's a good idea to store the StringCollection instead of calling the property every time.

What I did for example was something like that in order to create the base for an abstraction for such a setting:

public class RecentFilePropertyRepository
{
    private readonly StringCollection collection = RecentFiles.Default.MostRecentFiles;

    public StringCollection MostRecentFiles => collection;

    public void Save()
    {
        RecentFiles.Default.Save();
    }
}

For some reason unknown to me, updating the StringCollection from my property was resulting in having only the first update saved.

I either had to call RecentFiles.Default.MostRecentFiles = collection; before Save() or instead modify my property to call the "original" one:

public StringCollection MostRecentFiles => RecentFiles.Default.MostRecentFiles;

Internally the settings file returns the list like this

return ((global::System.Collections.Specialized.StringCollection)(this["MostRecentFiles"]));

The indexer call might have some implications that I'm unaware of.

Anyway, I understand that this isn't Francesco's problem, but if any other poor soul comes across the same issue, that might help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜