将 app.config 加载到 AppDomain 中 [英] Loading app.config into the AppDomain

查看:24
本文介绍了将 app.config 加载到 AppDomain 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将 App.Config 文件加载到应用程序域中.

I can't get the App.Config file to load into the App Domain.

我正在使用

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $config_path)

来自 Powershell 调用使用 App.config 的 .NET 程序集 但 App.Config 文件仍未加载.

from Powershell Calling .NET Assembly that uses App.config but the App.Config file is still not loaded.

我还尝试按照 使用 CurrentDomain.SetData("APP_CONFIG_FILE") 在 PowerShell ISE 中不起作用.

这是我的测试脚本:

$configFile = "{ActualPhysicalPath}\App.Config"
gc $configFile

Add-Type -AssemblyName System.Configuration
[Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
[Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)

[Configuration.ConfigurationManager]::ConnectionStrings[0].Name
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $null)
[Configuration.ConfigurationManager]::ConnectionStrings[0].Name
[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configFile)
[Configuration.ConfigurationManager]::ConnectionStrings[0].Name

我总是获取存储在 machine.config 中的连接字符串,而不是存储在 App.config 中的连接字符串.

I'm always getting the connection strings stored in machine.config, rather than those in the App.config.

如何在应用域中加载我的特定 App.Config 文件?

How can I get my specific App.Config file loaded in the app domain?

推荐答案

尝试将 SetData 语句移到 GetField 语句之前.

Try moving your SetData statement before the GetField statement.

使用 Windows 10 上的 PowerShell 5.0,您引用的链接 似乎有效:我可以检索AppSettingsConnectionStrings.

With PowerShell 5.0 on Windows 10, the guidance provided by the link you reference seems to work: I'm able to retrieve both AppSettings and ConnectionStrings.

Add-Type -AssemblyName System.Configuration

# Set this to the full path of your App.config
$configPath = "C:\Full\Path\To\App.config"

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configPath)
[Configuration.ConfigurationManager].GetField("s_initState", "NonPublic, Static").SetValue($null, 0)
[Configuration.ConfigurationManager].GetField("s_configSystem", "NonPublic, Static").SetValue($null, $null)
([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current", "NonPublic, Static").SetValue($null, $null)

[System.Configuration.ConfigurationManager]::AppSettings
[System.Configuration.ConfigurationManager]::ConnectionStrings

这篇关于将 app.config 加载到 AppDomain 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆