【问题标题】:Reload app.config section while app is running在应用程序运行时重新加载 app.config 部分
【发布时间】:2021-01-27 12:07:32
【问题描述】:

我一直在阅读和搜索如何使这项工作发挥作用,但我无法让我的应用重新加载或刷新它的加载设置。这是我的 exe.config 中的一个部分:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Tekla.Structures.Model" publicKeyToken="2f04dbe497b71114" culture="neutral" />
        <bindingRedirect oldVersion="17.0.0.0-99.1.0.0" newVersion="18.1.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

我需要将 newVersion 更改为 18.0.0.0,这很容易做到。问题是一旦我这样做了,如果不从头开始手动启动应用程序,我就无法让应用程序加载该信息。

我已经根据我的搜索尝试了所有这些组合,但似乎都不起作用:

System.Configuration.ConfigurationManager.RefreshSection("runtime");
System.Configuration.ConfigurationManager.RefreshSection("assemblyBinding");
System.Configuration.ConfigurationManager.RefreshSection("runtime/assemblyBinding");
System.Configuration.ConfigurationManager.RefreshSection("runtime/assemblyBinding/dependentAssembly");
System.Configuration.ConfigurationManager.RefreshSection("runtime/assemblyBinding/dependentAssembly/bindingRedirect");

我需要做什么才能重新加载?

【问题讨论】:

标签: c# app-config


【解决方案1】:

您只需在修改模式下保存配置文件,然后刷新这将使应用程序重新从磁盘读取文件。

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection("appSettings");

【讨论】:

  • @Alex:你是正确的绑定只在启动时解决。以上代码只刷新/重新加载配置文件。
  • 谢谢,似乎不可能不断更新绑定,所以我找到了另一个解决方案来解决我真正想做的事情。
猜你喜欢
  • 1970-01-01
  • 2011-09-14
  • 1970-01-01
  • 2012-05-19
  • 1970-01-01
  • 2012-02-11
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多