我试图在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
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
原文地址:https://www.jb51.cc/windows/372180.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。