.net-4.0 – 使用CurrentDomain.SetData(“APP_CONFIG_FILE”)在PowerShell ISE中不起作用

我试图在PowerShell ISE中使用.NET 4.0程序集,并尝试更改通过以下方式使用的配置文件
[System.AppDomain]::CurrentDomain.SetData("APP_CONfig_FILE",$PathToConfig);

[Configuration.ConfigurationManager] :: ConnectionStrings.Count始终返回“1”,“[Configuration.ConfigurationManager] :: ConnectionStrings [0] .Name”始终返回“LocalsqlServer”,并且该ConnectionString名称不在我的“.config”中文件.

请注意,从PowerShell命令提示符执行PowerShell脚本将按预期运行.就在我从PowerShell ISE中执行它时,它无法按预期工作.

这是因为PowerShell ISE的app.config路径已经加载并缓存,因此之后更改app.config路径不会产生任何影响:
stackoverflow.com/q/6150644/222748

这是一个示例脚本,它将清除缓存路径,以便它在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",$null)
([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"})[0].GetField("s_current",$null)
[Configuration.ConfigurationManager]::ConnectionStrings[0].Name

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...