Loading app.config into the AppDomain

后端 未结 2 1047
遇见更好的自我
遇见更好的自我 2021-02-13 10:23

I can\'t get the App.Config file to load into the App Domain.

I\'m using

[System.AppDomain]::CurrentDomain.SetData(\"APP_CONFIG_FILE\", $config_path)
         


        
2条回答
  •  清酒与你
    2021-02-13 10:46

    Try moving your SetData statement before the GetField statement.

    With PowerShell 5.0 on Windows 10, the guidance provided by the link you reference seems to work: I'm able to retrieve both AppSettings and ConnectionStrings.

    Add-Type -AssemblyName System.Configuration
    
    # Set this to the full path of your App.config
    $configPath = "C:\Full\Path\To\App.config"
    
    [System.AppDomain]::CurrentDomain.SetData("APP_CONFIG_FILE", $configPath)
    [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)
    
    [System.Configuration.ConfigurationManager]::AppSettings
    [System.Configuration.ConfigurationManager]::ConnectionStrings
    

提交回复
热议问题