应用程序设置中的StringCollection不会被存储 [英] StringCollection in application settings doesn't get stored

查看:278
本文介绍了应用程序设置中的StringCollection不会被存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用StringCollection作为应用程序设置,但是在阅读它不是问题,我发现设置不存储。

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?

代码我使用的是:

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


推荐答案

,问题是,如果你用new关键字创建你的StringCollection并保存设置,它们不会被存储。

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.

解决这个问题的方法是force 应用程序设置设计器为你创建它,怎么办?这很容易,把stringcollection作为类型和插入2/3字符串。按确定。然后再次编辑此值并删除所有字符串,将其保留为已创建但为空。

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!

这篇关于应用程序设置中的StringCollection不会被存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆