【问题标题】:Upgrade settings from old version with different name从具有不同名称的旧版本升级设置
【发布时间】:2018-11-25 13:57:14
【问题描述】:

我们使用 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...” 旧设置文件中不存在。我不确定为什么现在添加它,但如果我在复制时手动将它添加到旧设置文件中,它会起作用。

有没有办法让它工作?

【问题讨论】:

    标签: c# .net settings app-config smartassembly


    【解决方案1】:

    这可能不是正确的方法,但我只是编辑了 xml 以添加缺少的部分并且它正在工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 2020-04-30
      相关资源
      最近更新 更多