Search code examples
visual-studio-2008application-settings

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.Default.Save();

How can I use such a class as an application setting? I need a dictionary type collection as a setting...


Solution

  • There are a set of things i would like you to try.

    1. 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

    1. 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