c# - 从具有不同名称的旧版本升级设置
问题描述
我们使用 app.config 来存储用户设置,但是出于某种原因更改了 exe 的名称。我需要能够将设置从名为 name1.exe 的 1.4 版升级到现在名为 name2.exe 的 1.5 版。
我尝试了几种方法,第一种是在 Program.cs Main() 中添加代码,它将使用 System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath 来查找新设置的路径文件应该去,并搜索该设置的基本路径以在旧名称下找到 1.4 版本,然后将 1.4 文件夹复制到 Properties.Settings.Default.Upgrade() 正常工作的路径的正确部分。
我也试过用旧的替换新的 app.config 文件并调用 Properties.Settings.Default.Reload(); 并做同样的事情,但只是重新启动应用程序。
这些方法中的每一个都有效,即使在 ide 之外的发布模式下也是如此。但我们也使用 SmartAssembly 将其他一些 dll 合并到我们的 exe 程序集中。智能组装必须做一些未知的魔法,因为它在被 SmartAssembled 之后不起作用。
如果我手动将旧设置复制到新位置(替换 app.config)然后尝试运行我的应用程序甚至无法启动,它必须立即崩溃。同样,只有在使用 SmartAssembly 合并我们的 dll 之后。我已将崩溃范围缩小到 xml 中的这一部分...
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings">
<section name="DllNameA.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="DllNameB.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="ExeName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
xml“section name=ExeName.Properties.Settings...”的最后一部分不在旧设置文件中。我不确定为什么现在添加它,但是如果我在复制它时手动将它添加到旧设置文件中,它会起作用。
有没有办法使这项工作?
解决方案
这可能不是正确的方法,但我只是编辑了 xml 以添加缺少的部分并且它正在工作。
推荐阅读
- java - 在锁定任务模式下配对新设备 (Android P/10)
- typescript - 当我像参数一样使用它时,类型路由是什么?
- powershell - 如何将powershell响应复制到命令?
- .net - 如何在初始同步时跟踪 MongoDB Realm 中的进度?
- javascript - 如何求和并推送数组中的唯一项
- css - 如何使 vuetify stepper 全高
- android-studio - lateinit 属性 mUserViewModel 尚未初始化
- python - 根据条件映射列的值
- node.js - Mongoose 不在数组的 objectid 中填充 objectid
- javascript - 拼接不取消右行vuejs