![](/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.