The problem with the settings. NET Settings.Reload () and Settings.Save () does not load

I have a strange problem that I cannot understand. I have my own settings class that inherits ApplicationSettingsBase. It has user area settings as shown below.

    /// <summary>
    /// The Last Active Account
    /// </summary>
    [UserScopedSettingAttribute()]
    public AccountKeyClass ActiveAccount
    {
        get
        {
            try
            {
                return (AccountKeyClass)this["ActiveAccount"];
            }
            catch(Exception error){}
            return null;
        }
        set
        {
            this["ActiveAccount"] = value;
            if (!this.AccountList.Contains(value))
            {
                //this.AccountList.Add(value);
            }
        }
    }

    /// <summary>
    /// Account List
    /// Key: UserID
    /// Value: AccountKeyClass
    /// </summary>
    [UserScopedSettingAttribute()]
    public List<AccountKeyClass> AccountList
    {
        get
        {
            try
            {
                if(this["AccountList"] != null)
                    return (List<AccountKeyClass>)this["AccountList"]; 
            }
            catch(Exception error){}
            return null;
        }
        set { this["AccountList"] = value; }
    }

I have two forms: the main form and the settings form, in which you can change the application settings. When I create a SettingsForm and change the value of AccountList, the user.config file changes as excluded. My Apply / Ok button for my Form settings calls Settings.Save (), then Settings.Reload () closes the form. The problem is that when .Reload () is called, the Settings.AccountList list becomes null.

, user.config , , user.config - , Settings.AccountList .

Settings.AccountList , .Reload().

Update: List Save(); MainForm, AccountList user.config . AccountList (SettingsForm) Save(), , . , Reload().

, - Generic List < > . AccountKeyClass Serialized XML.

+3
2

.Reload(). ... - ( ).

+1

, AccountList ?

/ < > - Settings.Save() , , .

- :

var temp = settings.AccountList;
settings.AccountList = null;
settings.AccountList = temp;
settings.Save();
+1

Source: https://habr.com/ru/post/1736439/


All Articles