Search code examples
c#registrykeyassemblyinforecompileuser-preferences

New Windows C# Build Causes App to Lose Saved Preferences


Every time I recompile my Windows app, I update the AssemblyVersion and AssemblyFileVersion in AssemblyInfo.cs.

When my app loads, I call

Microsoft.Win32.RegistryKey key1 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Classes\\MyFileType\\shell\\open\\command", true);
           if (key1 == null)
           {
                Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\MyFileType\shell\open\command", null, "\"" + Application.ExecutablePath + "\"" + " \"%1\"");
                flag = true;
            }
            Microsoft.Win32.RegistryKey key2 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Classes\\.abc", true);
            if (key2 == null)
            {
                Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\.abc", null, "MyFileType");
                flag = true;
            }
            Microsoft.Win32.RegistryKey key3 = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Classes\\MyFileType\\DefaultIcon", true);
            if (key3 == null)
            {
                   Registry.SetValue(@"HKEY_CURRENT_USER\Software\Classes\MyFileType\DefaultIcon", "", Application.ExecutablePath + ",2");
                   flag = true;
            }
if (flag) SHChangeNotify(0x08000000, 0x0000, (IntPtr)null, (IntPtr)null);//SHCNE_ASSOCCHANGED SHCNF_IDLIST  

Every time my newly compiled app runs, all saved preferences are gone and the app reverts to the default preferences.

Any idea what triggers the app to lose it's preferences? Is it because I update the version number? If so, how do I get around that?

Is it because I reinsert the registry keys?

What could it be?

EDIT

The preferences load and save fine between instances of the app running, until I do a new build... but here's the code anyway:

 private void SaveSettings()
    {
        Properties.Settings.Default.useLastB = this.lastB.Checked;
        Properties.Settings.Default.useLast2B = this.last2B.Checked;
        Properties.Settings.Default.useLast3B = this.last3B.Checked;
        Properties.Settings.Default.useLastSV = this.lastSV.Checked;
        Properties.Settings.Default.useLast2SV = this.last2SV.Checked;
        Properties.Settings.Default.useLast3SV = this.last3SV.Checked;
        Properties.Settings.Default.showAverage = this.averageBox.Checked;
        Properties.Settings.Default.showSum = this.sumBox.Checked;
        Properties.Settings.Default.showNormalized = this.normalBox.Checked;
        Properties.Settings.Default.pessimistic = this.pessimistic.Checked;
        Properties.Settings.Default.realistic = this.realistic.Checked;
        Properties.Settings.Default.optimistic = this.optimistic.Checked;
        Properties.Settings.Default.useAinsley = this.ainsleyBox.Checked;
        Properties.Settings.Default.useHandicapper = this.handicapperBox.Checked;
        Properties.Settings.Default.useCustom = this.customBox.Checked;
        Properties.Settings.Default.UseMorningLine = this.useMLBox.Checked;
        Properties.Settings.Default.SolveAllRacesToSingleFile = this.singleFileCheckBox.Checked;

        Properties.Settings.Default.Save();
    }

    private void LoadSettings()
    {
        Properties.Settings.Default.Reload();
        this.lastB.Checked = Properties.Settings.Default.useLastB;
        this.last2B.Checked = Properties.Settings.Default.useLast2B;
        this.last3B.Checked = Properties.Settings.Default.useLast3B;
        this.lastSV.Checked = Properties.Settings.Default.useLastSV;
        this.last2SV.Checked = Properties.Settings.Default.useLast2SV;
        this.last3SV.Checked = Properties.Settings.Default.useLast3SV;
        this.averageBox.Checked = Properties.Settings.Default.showAverage;
        this.sumBox.Checked = Properties.Settings.Default.showSum;
        this.normalBox.Checked = Properties.Settings.Default.showNormalized;
        this.pessimistic.Checked = Properties.Settings.Default.pessimistic;
        this.realistic.Checked = Properties.Settings.Default.realistic;
        this.optimistic.Checked = Properties.Settings.Default.optimistic;
        this.handicapperBox.Checked = Properties.Settings.Default.useHandicapper;
        this.ainsleyBox.Checked = Properties.Settings.Default.useAinsley;
        this.customBox.Checked = Properties.Settings.Default.useCustom;
        this.useMLBox.Checked = Properties.Settings.Default.UseMorningLine;
        this.singleFileCheckBox.Checked = Properties.Settings.Default.SolveAllRacesToSingleFile;
    }

Thanks


Solution

  • The default location for the application settings is this path:

    %USERPROFILE%\Local Settings\Application Data\<Company Name>\<appdomainname>_<eid>_<hash>\<verison>\user.config
    

    Notice that the version is part of the path - this is why when you increment the version number, it appears as if the application has lost its settings.

    You can override where this is stored to remove the dependency on the version in the path - see Application Settings Architecture