c# - 如何使用特定的应用程序配置文件

标签 c# app-config

下面的代码不起作用。它不会从 TestApp.Config 文件中获取应用程序设置。你知道为什么吗?我该如何解决?

  public void GetConfig()
  {
     AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", @"C:\dev\VS\ProofOfConcept\ProofOfConcept\TestApp.config");
     var a = ConfigurationManager.AppSettings;
  }

最佳答案

post可能有帮助:

在此处发布的解决方案中,有一个方法 ResetConfigurationMechanism(),您应该在调用 CurrentDomain.SetData(...); 之后调用该方法。

private static void ResetConfigMechanism()
{
   typeof(ConfigurationManager)
   .GetField("s_initState", BindingFlags.NonPublic | BindingFlags.Static)                                                                                   
   .SetValue(null, 0);

   typeof(ConfigurationManager)
   .GetField("s_configSystem", BindingFlags.NonPublic | BindingFlags.Static)
   .SetValue(null, null);

   typeof(ConfigurationManager)
   .Assembly.GetTypes()
   .Where(x => x.FullName == 
      "System.Configuration.ClientConfigPaths")
   .First()
   .GetField("s_current", BindingFlags.NonPublic | BindingFlags.Static)
   .SetValue(null, null);
}

关于c# - 如何使用特定的应用程序配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15119132/

相关文章:

c# - 如何通过事件参数传递将事件绑定(bind)到 ViewModel 上的方法。银光4

c# - Windows Phone 8 检测屏幕解锁

c# - 仅包含私有(private)字段的流畅断言 Should().BeEquivalentTo

c# - 我需要在计时器刻度内的按钮单击对象上使用 if 语句吗?

c# - 从哈希字符串中获取原始密码

c# - 管理每个环境的配置

scala - 类型安全配置 : Load additional config from path external to packaged scala application

c# - C# 中的 PHP var_dump 转储数组或对象?

visual-studio-2010 - Visual Studio 总是为 App.config 选择错误的 xsd

C# ConfigurationManager 从 app.config 检索错误的连接字符串