views:

221

answers:

1

I'm creating a little console application that creates a Lucene index from an Sql Database. This application will be run with a single parameter. This parameter will define what connection string it will use and where the destination file should be placed.

I would like to store the connection strings and the target paths in the app.config file. Is it possible to group settings somehow? For example I would like that if the parameter "ABC" is given, connectionstring1 is used and targetPathBanana is used.

This following example doesn't work but I think illustrates my intention

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

I know I could just make the names of the keys follow a naming convention, but I'm curious if this could be solved in a way that's less convention-based.

A: 

If you use this prefix in your app.config file, you should be able to create as many custom section groups that contain <appSettings> and <connectionStrings> sections as you wish:

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

Cheers! Marc

marc_s
Thanks : )
borisCallens

related questions