开发者

Problem with .NET Settings.Reload() and Settings.Save() Settings not loading

开发者 https://www.devze.com 2022-12-23 01:23 出处:网络
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.

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<开发者_C百科;>. The AccountKeyClass that is being saved is saved as Serialized XML.


I recall a similar issue with .Reload(). Just for kicks... Try running it in release, or what ever is not debug mode in your setup (If you have not done so already).


please clarify if you are changing AccountList property or you are changing content of it?

If you add or remove item to/from List<> - Settings.Save() will not save it because it can't see that property has been changed.

Try something like this:

var temp = settings.AccountList;
settings.AccountList = null;
settings.AccountList = temp;
settings.Save();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号