c# - 使用 ConfigurationManager 的 OpenExeConfiguration 和 GetSection,(自定义配置文件和部分)

标签 c# .net

是否可以从 app.config 或 web.config 以外的配置文件中检索自定义配置部分。

我尝试同时调用 System.Configuration.ConfigurationManager 的 OpenExeConfiguration 和 GetSection 方法,但没有成功。我的意图是为可互换的过程适配器定义自定义配置部分,并将自定义配置部分包含在除 app.config 和 web.config 之外的单独配置文件中。我看到了很多应用程序设置和连接字符串的示例。

static private DigiKeyReadTaskConfigSection digiKeyReadTaskConfigSection;
  static DigiKeyReadTaskConfigSection DigiKeyReadTaskConfigSection {
     get {
        if (digiKeyReadTaskConfigSection == null) {
           digiKeyReadTaskConfigSection = (DigiKeyReadTaskConfigSection)ConfigurationManager.OpenExeConfiguration("ReadTask.config").GetSection("DigiKeyReadTaskConfigSection");
        }
        return digiKeyReadTaskConfigSection;
     }
  }

digiKeyReadTaskConfigSection = (DigiKeyReadTaskConfigSection)ConfigurationManager.OpenExeConfiguration 调用似乎有效,但是 (DigiKeyReadTaskConfigSection)ConfigurationManager.OpenExeConfiguration("ReadTask.config").GetSection("DigiKeyReadTaskConfigSection") 返回 null。

ReadTask.config 文件位于 App 的 bin 文件中:

   <configuration>   <configSections>
        <section name="DigiKeyReadTaskConfigSection" type="DataReadInc.WebSiteRead.TaskConfigSection.DigiKeyReadTaskConfigSection, DataReadInc.WebSiteRead" />
        <section name="ArrowReadTaskConfigSection" type="DataReadInc.WebSiteRead.TaskConfigSection.ArrowReadTaskConfigSection, DataReadInc.WebSiteRead" />   </configSections>   <DigiKeyReadTaskConfigSection DigiKeySiteURL="http://search.digikey.com/scripts/DkSearch/dksus.dll?WT.z_header=search_go&amp;lang=en&amp;site=us&amp;keywords="
                                    SiteLogInURL="https://ordering.digikey.com/RegisteredUser/Login.aspx,formName=" SiteLoginId="X" SiteLoginPassword="X" />   <ArrowReadTaskConfigSection ArrowAmericaSiteURL="http://components.arrow.com/part/search/"
                                      SiteLoginURL="http://components.arrow.com/login/processlogin#" SiteLoginId="X" SiteLoginPassword="X" /> </configuration>

我已经在 Spring.Net 和 J2EE 实现中看到了这种类型的设置,所以我确信这是可能的。我可以将我的自定义配置部分放在 App.config 或 web.config 文件中,但是将它们存在于自己的配置文件中会明显更干净。

最佳答案

使用ConfigurationManager.OpenMappedExeConfiguration() . OpenExeConfiguration 与某个exe相关。

关于c# - 使用 ConfigurationManager 的 OpenExeConfiguration 和 GetSection,(自定义配置文件和部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8974628/

相关文章:

c# - 帮助处理 excel 文件的 OleDB 连接字符串

C#简单除法问题

.net - 如果没有明确定义,aspnetcore 中服务的默认生命周期是多少?

.net - WPF根据对象属性在ContextMenu上隐藏MenuItem

c# - 如何在 .NET 中创建包含 'System' 的命名空间而不发生冲突?

c# - 如何简化 C# 中的代码?

c# - 为什么要异步回调?

c# - Application_AuthenticateRequest 和 FormsAuthentication_OnAuthenticate 有什么区别

c# - 在 .net 中管理和关闭动态创建的 SQL 连接

c# - 如果 C# .NET 出现错误,如何让我的代码跳过某些内容