![](/img/trans.png)
[英]How to write contents of System.Collections.Specialized.StringCollection to file?
[英]System.Collections.Specialized.StringCollection settings work fine in Debug and Release but crash on Deployment?
对于我最新的 WPF 应用程序,我一直在使用System.Collections.Specialized.StringCollection
来保存和加载字符串。 我当前的系统在 Debug 和 Release 中就像一个魅力,但是当部署到 ClickOnce 时,只要涉及到任何设置(加载或保存),应用程序就会崩溃! 但是,如果不是设置, System.Collections.Specialized.StringCollections
本身就可以正常工作。
什么可能导致这些崩溃?
这是我的系统:
public static void SaveCharacter(string code)
{
// Declare a blank StringCollection
StringCollection strings = new StringCollection();
// Convert the current settings StringCollection into an array and combine with the blank one
if (Settings.Default.SavedCharacters.Count > 0)
{
string[] tempArr= new string[Settings.Default.SavedCharacters.Count];
Settings.Default.SavedCharacters.CopyTo(tempArr, 0);
strings.AddRange(tempArr);
}
// Add the new string to the list
strings.Add(code);
// This new list is now saved as the setting itself
Settings.Default.SavedCharacters = strings;
Settings.Default.Save();
}
public static void RestoreCharacters()
{
foreach (string str in Settings.Default.SavedCharacters)
{
CreateCharacter(str, "l" + ID.ToString()); // This function works fine
ID++; // So does this variable (it's a public static)
}
}
PS 我用List<string>
项目尝试了类似的系统,但没有骰子。 不过,其他涉及strings
、 bools
和ints
工作正常。
PPS 旁注,有谁知道如何组合System.Collections.Specialized.StringCollections
而不必将其转换为数组?
回答我自己的问题,以防其他人遇到这样一个烦人的错误! 在您可以对必须是newed()
的用户设置进行操作之前,您需要创建它的一个实例来代替使用。 例如:
StringCollection foo = Settings.Default.SavedCharacters;
在保存此类集合时,只需使用Settings.Default.SavedCharacters = foo;
工作正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.