6

My app is written in WPF C# and I export it as Universal app using MSIX Application Project straight from Visual Studio.

I just can't get the settings to persist between updates. I'm using the following code in the MainWindow_Loaded event:

Settings.Default.Upgrade();
Settings.Default.Save();
Settings.Default.Reload();

I tried keeping assembly information versions the same and just increment the version in the appx.manifest but it doesn't work.

I've noticed that each time the app updates it creates a new uniquely named parent settings folder (with a new hash every time) and the subfolder name is the version from the assembly. The folder structure is like this:

App.exe_Url_dfvfmfjs1qo33zsag1ay5w1s0rwg0u53/0.2.10.0/user.config

App.exe_Url_tazrvujdga5ujjarnahpkoscv5zbkgl0/0.2.10.0/user.config

I believe it might have to do with the fact that it keeps generating new hashes instead of just placing the new version as a subfolder and that's why Upgrade doesn't do anything.

The only information I've found so far is to use Settings.Default.Upgrade()

How am I supposed to transfer the old version settings to the new version when my universal desktop bridge app updates?

Tony Steel
  • 123
  • 1
  • 6
  • Could you share more detail how do you make MSIX package with Visual Studio? – Nico Zhu May 14 '20 at 04:53
  • I followed this: [Link](https://learn.microsoft.com/en-us/windows/msix/desktop/desktop-to-uwp-packaging-dot-net) [Link](https://learn.microsoft.com/en-us/windows/msix/package/packaging-uwp-apps) Basically importing an MSIX Application Packaging Project and using it to deploy and publish by setting its target to my WPF project. – Tony Steel May 14 '20 at 10:18
  • I check the case update, and it is correct. – Nico Zhu May 15 '20 at 09:30
  • Okay so I suppose no better way to do it but write your own transfer methods (which copy to and from UWP settings) or just use both. I could try migrating all my settings to UWP settings and using that in ClickOnce deployment too but I'm unsure what the results would be (if settings will upgrade properly when updating the ClickOnce app). I'll post an update here if I try it. – Tony Steel May 15 '20 at 12:04

1 Answers1

0

As far as I researched these settings do not transfer to UWP updates using Desktop Bridge. So I started using UWP's native ApplicationData.Settings

As a workaround I created 2 methods to update the newly created WPF settings using LocalSettings which is the UWP equivalent and vice versa. UWP's LocalSettings transfer on update. I call Update() when I save my WPF settings and I call Load() when the application starts. It works.

Here's the code, only caveat I've found so far is you should use the basic types as these methods will fail transferring something like a List<string> or a StringCollection, for that I'm using serialization, although you can always adapt them to do that too:

static class UWPSettings
    {
        public static void Update()
        {
            if (Startup.IsUniversalPlatform)
            {
                foreach (SettingsPropertyValue value in Properties.Settings.Default.PropertyValues)
                {
                    ApplicationData.Current.LocalSettings.Values[value.Name] = value.PropertyValue;
                }
            }
        }

        public static void Load()
        {
            if (Startup.IsUniversalPlatform)
            {
                foreach (var setting in ApplicationData.Current.LocalSettings.Values)
                {
                    foreach (SettingsPropertyValue s in Properties.Settings.Default.PropertyValues)
                    {
                        if (s.Name == setting.Key)
                        {
                            s.PropertyValue = setting.Value;
                        }
                    }
                }

                Properties.Settings.Default.Save();
            }
        }
    }
Tony Steel
  • 123
  • 1
  • 6