0

I'm running my c# assembly form an Azure Runtime powershell, thus reading the assembly directly. The only problem with this is that it can't find the default app.config file.

To solve this issue, I was thinking to load the configuration directly into memory by giving the path to my config file. Is this possible, and how should I do this?

My goal is to read another file than app.config, for example mycustom.config into the configuration manager.

Solution: https://stackoverflow.com/a/6151688/1408786

Edit: this also works for me, saves me hardcoded config file locations in the source code.

[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
Community
  • 1
  • 1
1408786user
  • 1,868
  • 1
  • 21
  • 39
  • i am still trying to understand your question. At run time, the config file should be named with the same name as your assembly with the extension .config (they are different for web applications). Are you sure that even if you follow this convention, it is not loaded properly? – Thangadurai Apr 26 '17 at 08:41
  • When I start my application from visual studio it works because it takes the default config file. But the problem is when I load it as assembly it won't take the default config file. This is how I run it: Add-Type -Path "C:\Modules\User\MyAssemblyName\MyAssemblyName.dll" [MyAssemblyName.MyClassName]::MyStaticMethodName() So I thought, when I load in my method I'm calling from powershell the config file (I know the location), it should work. – 1408786user Apr 26 '17 at 08:43
  • I guess [this](http://stackoverflow.com/questions/17960/powershell-app-config) is what you are looking for. – Thangadurai Apr 26 '17 at 08:46
  • I've already tried that, but unfortunately it did not work out for me. – 1408786user Apr 26 '17 at 09:08
  • have you tried Your.App.Config in the csproj File? – EvertonMc Apr 26 '17 at 09:32
  • I have found a solution. This worked for me: http://stackoverflow.com/a/6151688/1408786 – 1408786user Apr 28 '17 at 11:43

0 Answers0