【发布时间】:2012-02-16 23:30:11
【问题描述】:
是否可以从 app.config 或 web.config 以外的配置文件中检索自定义配置部分。
我尝试同时使用 System.Configuration.ConfigurationManager 的 OpenExeConfiguration 和 GetSection 方法调用,但没有成功。我的目的是为可互换的流程适配器定义自定义配置部分,并将自定义配置部分包含在一个单独的配置文件中,而不是 app.config 和 web.config。我看到很多关于 appsettings 和 connectionstrings 的示例。
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&lang=en&site=us&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 文件中,但是将它们存在于自己的配置文件中会更干净。
【问题讨论】: