Can I group application keys?

I am creating a small console application that creates a Lucene index from an Sql database. This application will start with one parameter. This parameter will determine which connection string it will use and where the destination file should be located.

I would like to save the connection strings and destination paths in the app.config file. Is there any way to group the settings? For example, I would like for “ABC” to use connectionstring1 and targetPathBanana.

This following example does not work, but I think it 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 that I can simply specify the key names in accordance with the naming convention, but I am curious if this can be resolved in such a way that it is less based on conventions.

+3
1

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

Source: https://habr.com/ru/post/1710232/


All Articles