C# Problem with Settings.Reload() and Settings.Save() Settings not loading

Posted by galford13x on Stack Overflow See other posts from Stack Overflow or by galford13x
Published on 2010-03-11T06:00:36Z Indexed on 2010/03/12 0:47 UTC
Read the original article Hit count: 840

Filed under:
|

I have a strange issue that I can't figure out. I have a custom settings class that inherits ApplicationSettingsBase. It has a user scoped 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, a Main form and a Settings form in which to change application settings. When I creating the SettingsForm and change AccountList Settings value, the user.config file changes as excepected. My Apply/Ok button for my SettingsForm calls Settings.Save() then Settings.Reload() then closes the form. The problem is that when .Reload() is called, the Settings.AccountList becomes null.

Whats more is that the user.config file never changes, if i close the application and reopen, the user.config file is still correct, but the Settings.AccountList is never read in.

The Settings.AccountList is read in if i never call the .Reload() however.

Update: If I create a List and Save(); from my MainForm, then the AccountList will be read in from my user.config fine. However If I add to the AccountList using my secondary form (SettingsForm) and call Save() then the next time the application is run the settings are not read in and a null value is returned in it's place. This happens even if I do not use Reload().

I think the problem has something to do with using the Generic List<>. The AccountKeyClass that is being saved is saved as Serialized XML.

© Stack Overflow or respective owner

Related posts about settings

Related posts about c#