我已经使用Desktop Bridge的最新版本(通过Visual Studio 2017中的打包项目)将我的Windows窗体(桌面)应用程序转换为UWP,现在正在测试我的应用程序的UWP安装和升级过程。当我的应用程序通过先前的方法(ClickOnce / .MSI / Windows Installer)部署时,我的用户设置(首选项)在我的应用程序升级到更高版本时已正确迁移和保留。在UWP下,所有用户设置在升级时丢失/重置。我的问题与此主题中描述的问题相同(未提供解决方案):
Here is the prior thread I found, same issue, no resolution
有关如何正确保存通过桌面桥接到UWP的桌面应用程序的用户设置的任何想法?
谢谢!
答案 0 :(得分:2)
将数据存储在ApplicationData.LocalSettings。
中https://docs.microsoft.com/en-us/windows/uwp/design/app-settings/store-and-retrieve-app-data
答案 1 :(得分:0)
您可以将先前的user.config文件加载到当前设置中。这只是一种解决方法,可用于过渡到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.");
}