Loading app.config into the AppDomain Loading app.config into the AppDomain powershell powershell

Loading app.config into the AppDomain


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


Have you tried the solution presented here: Change default app.config at runtime

It appears to describe some sort of cache, and that more work has to be done to ensure the loaded data is read.

Th important part of the first answer is: "There exists a class ClientConfigPaths that caches the paths. So, even after changing the path with SetData, it is not re-read, because there already exist cached values. The solution is to remove these, too"