2012-01-23 16 views
2

app.configまたはweb.config以外の設定ファイルからカスタム設定セクションを取得することは可能ですか?ConfigurationManagerのOpenExeConfigurationとGetSectionの使用(カスタム設定ファイルとセクション)

私は、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")リターンを動作しているようです。

ReadTask.configファイルは、Appのbinファイルに住んでいる:私はそれが可能であると確信していますので、私はSpring.NetとセットアップとJ2EE実装のこのタイプを見てきました

<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> 

。私は自分のカスタム設定セクションをApp.configファイルまたはweb.configファイルに置くことができますが、独自の設定ファイルに存在するようにするためにはかなりクリーンです。

答えて

1

ConfigurationManager.OpenMappedExeConfiguration()を使用してください。 OpenExeConfigurationは特定のexeに関連しています。

+1

もう少し調べた後、別の方法を試しました。 ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap(); fileMap.ExeConfigFilename = "ReadTask.config"; 構成の設定= ConfigurationManager.OpenMappedExeConfiguration(fileMap、ConfigurationUserLevel.None); (DigiKeyReadTaskConfigSection).GetSection これは機能します! – user1165254

+0

@ user1165254 - これは正解です。ありがとう。 –

関連する問題