在应用程序设置中保存字典并在启动时加载它
Save dictionary in application settings and load it on start
我有 Dictionary<string, double>
,我想在重新启动应用程序时保存并恢复它。据我所知,您不能在 C# 中存储字典,因此尝试制定解决方法。
目前我在名为 DB
的设置中创建了字符串变量,并尝试使用如下内容:
var settings = dict.ToArray();
var prop = Properties.Settings.Default.DB;
然后制作类似prop = settings;
的东西
到目前为止我已经完成了这个,虽然它现在正在工作..
private void SaveSettings()
{
var settings = spamPercentage.ToArray();
var prop = Properties.Settings.Default.DB;
string res = String.Join(",", settings);
prop = res;
}
private void LoadSettings()
{
var prop = Properties.Settings.Default.DB;
var dictionary = prop.ToDictionary(item => item.Key,
item => item.Value);
}
编辑
字典中的值如下所示:
{[john, 0,53]}
{[ivone, 0,44]}
etc.
@编辑
使用 Julien JSON 的想法,我做了:
string prop = Properties.Settings.Default.DB;
private void SaveSettings()
{
prop = JsonConvert.SerializeObject(dict);
Properties.Settings.Default.Save();
}
private void LoadSettings()
{
dict= JsonConvert.DeserializeObject<Dictionary<string, double>>(prop);
}
当我点击程序并使用我的 SaveSettings 方法时,我可以看到它有值。然后我重新启动程序,并尝试加载设置,它们是空的。
前段时间我遇到了类似的问题。我最终使用 JSON Serialization 将字典序列化为字符串,将其存储在应用程序设置中,并在需要时将其反序列化回字典。
string strDictionaryAsString = JsonConvert.SerializeObject(dtnDictionary);
var dtnDictionary = JsonConvert.DeserializeObject<Dictionary<string, double>>(strDictionaryAsString );
我用它在 wpf 中存储我的数据网格设置
[XmlRoot("Dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
var keySerializer = new XmlSerializer(typeof(TKey));
var valueSerializer = new XmlSerializer(typeof(TValue));
bool wasEmpty = reader.IsEmptyElement;
reader.Read();
if (wasEmpty)
return;
while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
{
reader.ReadStartElement("item");
reader.ReadStartElement("key");
var key = (TKey)keySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement("value");
var value = (TValue)valueSerializer.Deserialize(reader);
reader.ReadEndElement();
this.Add(key, value);
reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
var keySerializer = new XmlSerializer(typeof(TKey));
var valueSerializer = new XmlSerializer(typeof(TValue));
foreach (TKey key in this.Keys)
{
writer.WriteStartElement("item");
writer.WriteStartElement("key");
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
writer.WriteStartElement("value");
TValue value = this[key];
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
#endregion
}
设置:
[SettingsManageabilityAttribute(SettingsManageability.Roaming)]
public abstract class GridSettingsBase : ApplicationSettingsBase, IGridSettings
{
[UserScopedSettingAttribute()]
[DefaultSettingValue("")]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public SerializableDictionary<string, int> GridDisplayIndexList
{
get { return (SerializableDictionary<string, int>)this["GridDisplayIndexList"]; }
set { this["GridDisplayIndexList"] = value; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValue("")]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public SerializableDictionary<string, Visibility> GridColumnVisibilityList
{
get { return (SerializableDictionary<string, Visibility>)this["GridColumnVisibilityList"]; }
set { this["GridColumnVisibilityList"] = value; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValue("")]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public SerializableDictionary<string, double> GridColumnWidthList
{
get { return (SerializableDictionary<string, double>)this["GridColumnWidthList"]; }
set { this["GridColumnWidthList"] = value; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValue("")]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public List<SortDescription> GridSortDescriptionList
{
get { return (List<SortDescription>)this["GridSortDescriptionList"]; }
set { this["GridSortDescriptionList"] = value; }
}
protected GridSettingsBase()
{
Application.Current.Exit += OnExit;
}
private void OnExit(object sender, ExitEventArgs e)
{
this.Save();
}
}
我有 Dictionary<string, double>
,我想在重新启动应用程序时保存并恢复它。据我所知,您不能在 C# 中存储字典,因此尝试制定解决方法。
目前我在名为 DB
的设置中创建了字符串变量,并尝试使用如下内容:
var settings = dict.ToArray();
var prop = Properties.Settings.Default.DB;
然后制作类似prop = settings;
到目前为止我已经完成了这个,虽然它现在正在工作..
private void SaveSettings()
{
var settings = spamPercentage.ToArray();
var prop = Properties.Settings.Default.DB;
string res = String.Join(",", settings);
prop = res;
}
private void LoadSettings()
{
var prop = Properties.Settings.Default.DB;
var dictionary = prop.ToDictionary(item => item.Key,
item => item.Value);
}
编辑 字典中的值如下所示:
{[john, 0,53]}
{[ivone, 0,44]}
etc.
@编辑 使用 Julien JSON 的想法,我做了:
string prop = Properties.Settings.Default.DB;
private void SaveSettings()
{
prop = JsonConvert.SerializeObject(dict);
Properties.Settings.Default.Save();
}
private void LoadSettings()
{
dict= JsonConvert.DeserializeObject<Dictionary<string, double>>(prop);
}
当我点击程序并使用我的 SaveSettings 方法时,我可以看到它有值。然后我重新启动程序,并尝试加载设置,它们是空的。
前段时间我遇到了类似的问题。我最终使用 JSON Serialization 将字典序列化为字符串,将其存储在应用程序设置中,并在需要时将其反序列化回字典。
string strDictionaryAsString = JsonConvert.SerializeObject(dtnDictionary);
var dtnDictionary = JsonConvert.DeserializeObject<Dictionary<string, double>>(strDictionaryAsString );
我用它在 wpf 中存储我的数据网格设置
[XmlRoot("Dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
var keySerializer = new XmlSerializer(typeof(TKey));
var valueSerializer = new XmlSerializer(typeof(TValue));
bool wasEmpty = reader.IsEmptyElement;
reader.Read();
if (wasEmpty)
return;
while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
{
reader.ReadStartElement("item");
reader.ReadStartElement("key");
var key = (TKey)keySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement("value");
var value = (TValue)valueSerializer.Deserialize(reader);
reader.ReadEndElement();
this.Add(key, value);
reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
var keySerializer = new XmlSerializer(typeof(TKey));
var valueSerializer = new XmlSerializer(typeof(TValue));
foreach (TKey key in this.Keys)
{
writer.WriteStartElement("item");
writer.WriteStartElement("key");
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
writer.WriteStartElement("value");
TValue value = this[key];
valueSerializer.Serialize(writer, value);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
#endregion
}
设置:
[SettingsManageabilityAttribute(SettingsManageability.Roaming)]
public abstract class GridSettingsBase : ApplicationSettingsBase, IGridSettings
{
[UserScopedSettingAttribute()]
[DefaultSettingValue("")]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public SerializableDictionary<string, int> GridDisplayIndexList
{
get { return (SerializableDictionary<string, int>)this["GridDisplayIndexList"]; }
set { this["GridDisplayIndexList"] = value; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValue("")]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public SerializableDictionary<string, Visibility> GridColumnVisibilityList
{
get { return (SerializableDictionary<string, Visibility>)this["GridColumnVisibilityList"]; }
set { this["GridColumnVisibilityList"] = value; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValue("")]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public SerializableDictionary<string, double> GridColumnWidthList
{
get { return (SerializableDictionary<string, double>)this["GridColumnWidthList"]; }
set { this["GridColumnWidthList"] = value; }
}
[UserScopedSettingAttribute()]
[DefaultSettingValue("")]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public List<SortDescription> GridSortDescriptionList
{
get { return (List<SortDescription>)this["GridSortDescriptionList"]; }
set { this["GridSortDescriptionList"] = value; }
}
protected GridSettingsBase()
{
Application.Current.Exit += OnExit;
}
private void OnExit(object sender, ExitEventArgs e)
{
this.Save();
}
}