![](/img/trans.png)
[英]Using ConfigurationManager.GetSection in a Class Library's App.config
[英]Using ConfigurationManager's OpenExeConfiguration and GetSection,(Custom config file and section)
是否可以从app.config或web.config以外的配置文件中检索自定义配置节。
我尝试使用System.Configuration.ConfigurationManager的OpenExeConfiguration和GetSection方法调用,但没有运气。 我的目的是为可互换的进程适配器定义自定义配置节,并在除app.config和web.config之外的单独配置文件中包含自定义配置节。 我看到很多关于appsettings和connectiontrings的例子。
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有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.