Using CurrentDomain.SetData(“APP_CONFIG_FILE”) doesn't work in PowerShell ISE

前端 未结 2 852
暗喜
暗喜 2020-12-31 18:11

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.SetDa         


        
相关标签:
2条回答
  • 2020-12-31 18:41

    Taking off [0] works for me.

    ([Configuration.ConfigurationManager].Assembly.GetTypes() | where {$_.FullName -eq "System.Configuration.ClientConfigPaths"}).GetField("s_current", "NonPublic, Static").SetValue($null, $null)

    0 讨论(0)
  • 2020-12-31 18:47

    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

    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
    
    0 讨论(0)
提交回复
热议问题