2009-06-11 13 views
3

私はSQLデータベースからLuceneインデックスを作成する小さなコンソールアプリケーションを作成しています。 このアプリケーションは単一のパラメータで実行されます。このパラメータは、使用する接続文字列と宛先ファイルを配置する場所を定義します。設定キーをグループ化することはできますか?

接続文字列とターゲットパスをapp.configファイルに保存します。 設定を何らかの方法でグループ化することはできますか?たとえば、パラメータ「ABC」が指定されている場合は、connectionstring1が使用され、targetPathBananaが使用されます。

この次の例では動作しませんが、私は私の意図

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <abc> 
     <appSettings>    
      <add key="targetBasePath" value="\\Thor\lucene\abc"/> 
     </appSettings> 
     <connectionStrings>    
      <add name="commonString" 
       connectionString="Data Source=thor;Persist Security Info=True;User ID=****;Password=****"/> 
     </connectionStrings> 
    </abc>  
    <123> 
     <appSettings>    
      <add key="targetBasePath" value="\\Loki\temp\lucene"/> 
     </appSettings> 
     <connectionStrings>    
      <add name="commonString" 
       connectionString="Data Source=helga;Persist Security Info=True;User ID=****;Password=****"/> 
     </connectionStrings> 
    </123> 
</configuration> 

を示して、私はちょうどキーの名前が命名規則に従いますが、これを解決することができれば、私は好奇心が強い作ることができる知っていると思います慣習に基づいた方法ではありません。

答えて

4

あなたのapp.configファイルにこのプレフィックスを使用する場合は、あなたが望むように<appSettings><connectionStrings>セクションが含まれているとして、多くのカスタムセクション・グループを作成することができるはずです。

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <sectionGroup name="ABC"> 
     <section name="appSettings" 
       type="System.Configuration.AppSettingsSection, 
        System.Configuration"/> 
     <section name="connectionStrings" 
       type="System.Configuration.ConnectionStringsSection, 
        System.Configuration"/> 
    </sectionGroup> 
    </configSections> 
    ... put your section groups here..... 
    <ABC> 
    <appSettings>       
     <add key="targetBasePath" value="\\Thor\lucene\abc"/> 
    </appSettings> 
    <connectionStrings>      
     <add name="commonString" connectionString="..."/> 
    </connectionStrings> 
    </ABC> 
</configuration> 
+0

ありがとう:) –

+0

上の任意の例'ConfigurationManager.AppSettings [@" ??? "]でグループ化されたappsettingsにアクセスするにはどうすればいいですか? –

+0

グループへのアクセス方法の例を追加してください。 –

関連する問題