I've got Dictionary<string, double>
which I would like to save and restore it when I restart the application. From what I have heard you cannot store dictionary in c#, so trying to make a workaround.
Currently I have made string variable in setting called DB
, and trying to use something like this:
var settings = dict.ToArray();
var prop = Properties.Settings.Default.DB;
and make something like prop = settings;
Thus far I have done this, although it is now working..
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);
}
edit Values in dictionary looks like this:
{[john, 0,53]}
{[ivone, 0,44]}
etc.
@edit Using Julien JSON idea, I have made:
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);
}
When I hit program, and use my SaveSettings method, I can see that it has values. Then I reboot program, and try loadsettings and they are null.