When installing a new update of a .Net application with a VS.Net project there will often be a requirement to preserve some of the existing application settings. The easiest way (and only way so far) I found to do this was to use the Settings VS.Net template and specify the settings that need to be updated as UserScoped. The application can then perform a check each time it is run and if it detects itself being updated can run an Update method to preserve any UserScoped settings from a previous version.

  • Add a new Settings item to the application being installed. Add all the required settings to it. Add an additional setting called ApplicationVersion. The Settings item template generates a VS.Net code file and also an App.Config file, it will also detect changes between the two and attempt to keep them in sync. When deployed on a client machine the settings file does not get deployed as the standard executable.exe.config and is instead stored in a directory under the user’s home directory,
  • When an application update occurs with an msi file a new settings file is created based on the values in the original VS.Net settings file. However, and importantly, the settings file for the previous version are not overwritten, each install creates a unique settings file name,
  • By adding a small code snippet to the application that gets performed each time it starts up it’s possible for the application to pull the settings from the previous application version into the newly installed one and thus preserve them.

            AppSettings appSettings = new AppSettings();
            
            System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
            Version appVersion = a.GetName().Version;
            string appVersionString = appVersion.ToString();

            if (appSettings.ApplicationVersion != appVersion.ToString())
            {
                appSettings.Upgrade();
                appSettings.ApplicationVersion = appVersionString;
                appSettings.Save();
                Console.WriteLine("Settings file was upgraded.");
            }

Advertisement