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

问题描述

我无法将App.Config文件加载到App Domain中.

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

我正在使用

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

来自使用App.config的Powershell调用.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,似乎有效:我能够检索 AppSettings ConnectionStrings .

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中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 00:58