Elkland Technologies
Elkland Technologies

Reputation: 65

Windows Forms to UWP app via Desktop Bridge - User Settings are lost/reset on UWP app Upgrade

I have converted my Windows Forms (desktop) app to UWP using the latest iteration of the Desktop Bridge (via a Packaging Project in Visual Studio 2017) and am now testing the UWP install and upgrade processes for my app. When my app was deployed via the prior methodology (ClickOnce / .MSI / Windows Installer) my User Settings (preferences) were properly migrated and preserved when my app was upgraded to a later version. Under UWP all User Settings are lost/reset on upgrade. My issue is the same issue that was described in this thread (for which no resolution was provided):

Here is the prior thread I found, same issue, no resolution

Any ideas on how to properly preserve User Settings for a Desktop app brought to UWP via the Desktop Bridge?

Thanks!

Upvotes: 0

Views: 550

Answers (2)

Trympet
Trympet

Reputation: 75

You could load the previous user.config file into current settings. This is just a workaround, and can be used to transition to ApplicationData.LocalSettings.

public static void Init() {
    LoadPreviousSettings(ApplicationSettings.Default, MyFancySettings.Default);
}

private static void LoadPreviousSettings(params ApplicationSettingsBase[] applicationSettings)
{
    const string companyName = "YOUR_COMPANY_NAME_HERE";
    var userConfigXml = GetUserConfigXml(companyName);
    Configuration config = ConfigurationManager.OpenExeConfiguration(
        ConfigurationUserLevel.PerUserRoamingAndLocal);
    foreach (ApplicationSettingsBase setting in applicationSettings)
    {
        try
        {
            // loads settings from user.config into configuration
            LoadSettingSection(setting, config, userConfigXml);
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("userSettings");
        }
        catch (FileNotFoundException)
        {
            // Could not import settings.
        }
        setting.Reload();
    }

}

private static void LoadSettingSection(ApplicationSettingsBase setting, Configuration config, XDocument userConfigXml)
{
    string appSettingsXmlName = setting.ToString();
    var settings = userConfigXml.XPathSelectElements("//" + appSettingsXmlName);
    config.GetSectionGroup("userSettings")
        .Sections[appSettingsXmlName]
        .SectionInformation
        .SetRawXml(settings.Single().ToString());
}

private static XDocument GetUserConfigXml(string companyName)
{
    var localPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + $@"\{companyName}";
    // previous package folder
    var previousLocal = GetDirectoryByWriteTime(localPath, 1);
    // previous version, e.g. 1.2.0
    var prevousVersion = GetDirectoryByWriteTime(previousLocal, 0);
    // application settings for previous version
    return XDocument.Load(prevousVersion + @"\user.config");
}

private static string GetDirectoryByWriteTime(string path, int order)
{
    var direcotires = new DirectoryInfo(path).EnumerateDirectories()
        .OrderBy(d => d.LastWriteTime)
        .Reverse()
        .ToList();
    if (direcotires.Count > order)
    {
        var previous = direcotires[order];
        return previous.FullName;
    }
    throw new FileNotFoundException("Previous config file not found.");
}

Upvotes: 0

Stefan Wick  MSFT
Stefan Wick MSFT

Reputation: 13850

Store the data in ApplicationData.LocalSettings.

https://learn.microsoft.com/en-us/windows/uwp/design/app-settings/store-and-retrieve-app-data

Upvotes: 4

Related Questions