加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

.net-4.0 – 使用CurrentDomain.SetData(“APP_CONFIG_FILE”)在

发布时间:2020-12-13 20:44:52 所属栏目:Windows 来源:网络整理
导读:我试图在PowerShell ISE中使用.NET 4.0程序集,并尝试更改通过以下方式使用的配置文件: [System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE",$PathToConfig); [Configuration.ConfigurationManager] :: ConnectionStrings.Count始终返回“1”,“[C
我试图在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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读