应用程序设置中的StringCollection不会被存储

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

如何使其工作? 任何解决方法? 这有什么问题?

我正在使用的代码:

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并保存设置,它们就不会被存储。

解决这个问题的方法是“强制”应用程序设置设计者为您创建它,如何做到这一点? 好吧,这很简单,把stringcollection作为类型并插入2/3字符串。 按确定。 然后再次编辑此值并删除所有字符串以使其“创建但为空”。

在此之后,您可以通过添加/删除字符串并保存设置来使用它。 你肯定它不会是空的!

应用程序设置可以在应用程序级别和用户级别进行范围设定 ,您只能在用户级别写入设置,因此如果在应用程序级别有一个StringCollection范围,则只能读取在编译时定义的值和在下次启动应用程序时,在运行时添加到集合将不起作用。

如果希望更改在应用程序运行之间传播,则可以在用户级别对其进行范围调整。