首页 > 解决方案 > 从具有不同名称的旧版本升级设置

问题描述

我们使用 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#.netsettingsapp-configsmartassembly

解决方案


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


推荐阅读