在程序更新(Desktop Bridge / Project Centennial)上升级WPF程序设置的正确方法

时间:2016-12-30 05:37:43

标签: wpf uwp windows-10-universal project-centennial desktop-bridge

当它是一个clickonce程序时,它工作,但后来我制作了一个appxpackage并将其导出为Windows商店的百年应用程序,升级不再起作用。

现在我在App.xaml.cs

|

将UpgradeRequired用作用户设置中的bool。那是对的吗?

我在每个版本更新时重置设置。现在我有几个这样的目录

  

C:\用户\我\应用程序数据\本地\的progname \ prog.exe_Url_randomChars

每个都有几个不同版本的程序设置。因此,在升级之后,将创建另一个,而不是当前版本的x.x.x.x的子文件夹。

和以前一样,在每个版本的发行版中,我都会在程序集信息中增加程序集版本,文件版本的版本,现在我在AppxManifest.xml中有相同的编号。我按照建议将版本的最后一个数字组保持为0,并且只增加第3个数字组。

我有什么遗失的吗?

3 个答案:

答案 0 :(得分:0)

UWP和桌面桥应用程序需要将其设置存储在ApplicationData.LocalSettings:

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

答案 1 :(得分:0)

您可以将先前的user.config文件加载到当前设置中。这只是一种解决方法,可用于过渡到ApplicationData.LocalSettings。

public static void Init()
{
    if (myprog.Properties.Settings.Default.UpgradeRequired)
    {    
        LoadPreviousSettings(myprog.Properties.Settings.Default);
        myprog.Properties.Settings.Default.UpgradeRequired = false;
        myprog.Properties.Settings.Default.Save();
    }
}

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.
            // Perhaps user has no previous version installed
        }
        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.");
}

答案 2 :(得分:0)

有一个有效的answer here

基本上,您需要使用UWP的ApplicationData.Settings创建一个重复的版本,然后在应用程序的开头加载它。当您的设置是字符串,布尔值等时,这非常简单。但是如果您有唯一的设置,则不是这样。

要详细说明链接中的答案,当您具有由自定义类型/类组成的设置时,在创建UWP的重复版本时,可以使用Newtonsoft.Json序列化自定义设置:

try
{
    ApplicationData.Current.LocalSettings.Values[value.Name] = value.PropertyValue;
}
catch
{
    string serialised = JsonConvert.SerializeObject(value.PropertyValue);
    ApplicationData.Current.LocalSettings.Values[value.Name] = serialised;
}

然后在加载自定义设置时:

if (s.Name == "MyCustomSetting")
{
    var deserialised = JsonConvert.DeserializeObject<MyCustomClass>((string)setting.Value);
    s.PropertyValue = deserialised;
}