C# 从默认app.config以外的其他配置文件读取

C# 从默认app.config以外的其他配置文件读取,c#,powershell,azure,app-config,C#,Powershell,Azure,App Config,我正在Azure运行时powershell中运行我的c#程序集表单,从而直接读取程序集。唯一的问题是它找不到默认的app.config文件 为了解决这个问题,我想通过提供配置文件的路径,将配置直接加载到内存中。这可能吗?我该怎么做 我的目标是将app.config以外的另一个文件(例如mycustom.config)读入configuration manager 解决方案: 编辑: 这也适用于我,将我的硬编码配置文件位置保存在源代码中 [System.AppDomain]::CurrentDo

我正在Azure运行时powershell中运行我的c#程序集表单,从而直接读取程序集。唯一的问题是它找不到默认的app.config文件

为了解决这个问题,我想通过提供配置文件的路径,将配置直接加载到内存中。这可能吗?我该怎么做

我的目标是将app.config以外的另一个文件(例如mycustom.config)读入configuration manager

解决方案:

编辑: 这也适用于我,将我的硬编码配置文件位置保存在源代码中

[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

我仍在努力理解你的问题。在运行时,配置文件的名称应与程序集的名称相同,扩展名为.config(对于web应用程序,它们是不同的)。您确定即使遵循此约定,它也没有正确加载吗?当我从visual studio启动应用程序时,它可以工作,因为它使用默认的配置文件。但问题是,当我将其作为程序集加载时,它不会采用默认的配置文件。这就是我运行它的方式:添加Type-Path“C:\Modules\User\MyAssemblyName\MyAssemblyName.dll”[MyAssemblyName.MyClassName]::MyStaticMethodName(),所以我想,当我在方法中加载时,我从powershell调用配置文件(我知道位置),它应该可以工作。我猜这就是您要查找的。我已经尝试过了,但不幸的是,它对我不起作用。你在csproj文件中尝试过你的.App.Config吗?