使用CurrentDomain.SetData(" APP_CONFIG_FILE")在PowerShell ISE中不工作 [英] Using CurrentDomain.SetData("APP_CONFIG_FILE") doesn't work in PowerShell ISE

查看:709
本文介绍了使用CurrentDomain.SetData(" APP_CONFIG_FILE")在PowerShell ISE中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用.NET 4.0组件PowerShell ISE中,并试图改变它是通过使用配置文件:

I'm attempting to use a .NET 4.0 assembly in PowerShell ISE, and trying to change the config file which is used via:

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

[Configuration.ConfigurationManager] :: ConnectionStrings.Count总是1,
和[Configuration.ConfigurationManager] ::的ConnectionStrings [0] .Name点总是返回LocalSqlServer这个,那ConnectionString的名称是不是在我的.config文件。

[Configuration.ConfigurationManager]::ConnectionStrings.Count always returns "1",
and "[Configuration.ConfigurationManager]::ConnectionStrings[0].Name" always returns "LocalSqlServer", and that ConnectionString name is not in my ".config" file.

请注意,从一个PowerShell执行PowerShell脚本命令提示符下按预期。这只是当我从PowerShell的ISE中执行它如预期,这是行不通的。

Note that executing the PowerShell script from a PowerShell command prompt functions as expected. It's just when I execute it from within PowerShell ISE, it doesn't work as expected.

推荐答案

这是因为App.config中为PowerShell ISE中的路径已经被加载并缓存,以便改变在app.config路径之后不会有所作为: stackoverflow.com/q/6150644/222748

It's because the path to app.config for PowerShell ISE has already been loaded and cached so changing the app.config path afterwards won't make a difference: stackoverflow.com/q/6150644/222748

下面是一个例子脚本,将清除缓存的路径,它会在PowerShell ISE中工作:

Here is an example script that will clear the cached path so it will work under PowerShell ISE:

[System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $PathToConfig)
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

这篇关于使用CurrentDomain.SetData(" APP_CONFIG_FILE")在PowerShell ISE中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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