简体   繁体   中英

User scope app settings always reset to defaults

I have a WinForms app that uses the Settings feature of .NET 2, but the User scope settings values saved in the application often get replaced by the default values stored in the Settings class. Long ago I found guidance somewhere that attributed this to an auto-incrementing build number, and I suspect this is true. However, said guidance suggested the following code, but somehow my settings still keep getting reset.

I'm solving too many other issues at the moment to go into this any deeper than this, bu tI thought I'd through the question out and see if someone can help me, and everyone else with this problem.

    private void Form1_Load(object sender, EventArgs e)
    {
        UpgradeSettings();
        GetSettings();
    }

    private void UpgradeSettings()
    {
        if (Properties.Settings.Default.CallUpgrade)
        {
            Properties.Settings.Default.Upgrade();
            Properties.Settings.Default.CallUpgrade = false;
        }
    }

You have to modify UpgradeSettings as follows. The initial value of CallUpgrade should be true (in the User Settings).

private void Form1_Load(object sender, EventArgs e)
{
    UpgradeSettings();
    GetSettings();
}

private void UpgradeSettings()
{
    if (Properties.Settings.Default.CallUpgrade)
    {
        Properties.Settings.Default.Upgrade();
        Properties.Settings.Default.Reload(); // to activate the settings
        Properties.Settings.Default.CallUpgrade = false;
        Properties.Settings.Default.Save();// to save the new value of CallUpgrade            
    }
}

Does CallUpgrade default to true? The call to Upgrade() should migrate settings across build numbers.

Another approach is to use fixed build numbers. You can manually modify your AssemblyInfo.cs as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM