我一直在Properties.Settings.Default
对象中存储用户设置的集合,并使用Visual Studio设置设计器(右键单击您的项目,单击属性,然后单击设置选项卡)来设置。最近,一些用户抱怨说,这些特定设置跟踪的数据随机丢失。这是滥用设置功能吗?
举一个想法(不正是我是怎么做的,但有些接近),它的工作方式是我有一个对象,像这样:
class MyObject
{
public static string Property1 { get; set; }
public static string Property2 { get; set; }
public static string Property3 { get; set; }
public static string Property4 { get; set; }
}
然后在代码中,我可能会做这样的事情这个保存信息:
public void SaveInfo()
{
ArrayList userSetting = new ArrayList();
foreach (Something s in SomeCollectionHere) // For example, a ListView contains the info
{
MyObject o = new MyObject {
Property1 = s.P1;
Property2 = s.P2;
Property3 = s.P3;
Property4 = s.P4;
};
userSetting.Add(o);
}
Properties.Settings.Default.SettingName = userSetting;
}
现在,代码拉出来是这样的:
public void RestoreInfo()
{
ArrayList setting = Properties.Settings.Default.SettingName;
foreach (object o in setting)
{
MyObject data = (MyObject)o;
// Do something with the data, like load it in a ListView
}
}
我也确信装饰Settings.Designer.cs与[global::System.Configuration.SettingsSerializeAs(global::System.Configuration.SettingsSerializeAs.Binary)]
文件像这样:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.SettingsSerializeAs(global::System.Configuration.SettingsSerializeAs.Binary)]
public global::System.Collections.ArrayList SettingName
{
get {
return ((global::System.Collections.ArrayList)(this["SettingName"]));
}
set {
this["SettingName"] = value;
}
}
现在,随机,信息就会消失。我可以调试,看到Properties.Settings.Default
正在为SettingName
返回一个空的ArrayList
。我真的不想使用ArrayList,但我没有看到以这种方式获取泛型集合的方法。
我即将放弃并使用纯XML保存这些信息。我只是想验证我确实推动了这一点.NET基础设施。我对吗?
不幸的是,汇编版本没有改变,当新版本发布时,设置已经升级。 – jasonh 2009-10-07 01:11:03