是否可以从 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&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 文件中,但是将它们存在于自己的配置文件中会明显更干净。
最佳答案
使用ConfigurationManager.OpenMappedExeConfiguration() . OpenExeConfiguration
与某个exe相关。
关于c# - 使用 ConfigurationManager 的 OpenExeConfiguration 和 GetSection,(自定义配置文件和部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8974628/