Visual Studio 2008 application settings not saved?
I know:
Application settings can be stored as any data type that is XML serializable or has a TypeConverter that implements ToString/FromString. The most common types are String, Integer, and Boolean, but you can also store values as Color, Object, or as a connection string.
I have a ListDictionary class setting - which is serializable but everytime I start up my app again it is null despite creating, assigning and saving a ListDictionary to my settings:
Properties.Settings.Default.Preferences = new System.Collections.Specialized.ListDictionary();
Properties.Settings.Default.Preferences.Add(1, "test");
Properties.Settings.De开发者_开发知识库fault.Save();
How can I use such a class as an application setting? I need a dictionary type collection as a setting...
There are a set of things i would like you to try.
- make sure you create the settings scope as USER .
http://msdn.microsoft.com/en-us/library/aa730869(v=vs.80).aspx add 2 strings(DictionaryKey and Dictionaryvalue) to the settings and set the scope as user and value as blank
- Settings doesn't contain an option to add Dictionary . So what i would suggest is try this code
make 2 sting settings and do this
Properties.Settings.Default.Dictionarykey = "";// To empty previous settings
Properties.Settings.Default.Dictionaryvalue = "";// To empty previous settings
for (int i = 0; i < yourdictionarycount; i++ )
{
Properties.Settings.Default.Dictionarykey += i + "#"; // or any other value
Properties.Settings.Default.Dictionaryvalue += "test" + "#"; // or any other value
}
Properties.Settings.Default.Save();
And when you are retrieving the values
use this code:
public Dictionary<int, string> savedsettings = new Dictionary<int, string>();
string[] eachkey = Properties.Settings.Default.Dictionarykey.Split('#');
string[] value = Properties.Settings.Default.Dictionaryvalue.Split('#');
for (int j = 0; j < eachkey.Length; j++)
{
savedsettings.Add(eachkey[i], eachvalue[i]);
}
//just to check if the values are being retrieved properly
Messagebox.Show(Properties.Settings.Default.Dictionarykey);
Regards,
Vivek Sampara
精彩评论