How to create a new configSections in App.config

4

I'm working with an application that currently uses the appSettings section to get application settings through ConfigurationManager.AppSettings .

But I would like a more complex structure for my configuration, something like this:

<configuration>
  <externalConnections>
    <connection name="Conn1" type="sharepoint">
      <username value="login">
    </connection>
    <connection name="Conn2" type="sap">
      <username value="login">
      <password value="password">
    </connection>
  </externalConnections>
</configuration>

I saw something related to configSections but I do not know how to create a class to use this type of configuration.

How can I create this configuration class? How can this class be used from ConfigurationManager.AppSettings also?

    
asked by anonymous 28.09.2015 / 22:16

1 answer

0

Good afternoon,

I had to store some information about an application for some time, it's environment settings, it's very simple

    //Instanciando e utilizando o app.conf default da aplicação
    Configuration config = ConfigurationManager.OpenExeConfiguration(System.Reflection.Assembly.GetExecutingAssembly().Location);

    //adicionado a chave e valor    
    config.AppSettings.Settings.Add("ConfiguracaoHorario",comboBox_configuracao_horario.Text.ToString());

    //adicionado a chave e valor  
    config.AppSettings.Settings.Add("IP_Servidor",txt_servidor_banco_dados.Text.ToString());


     //salvando as configurações
     config.Save(ConfigurationSaveMode.Modified);


    //desta forma será armazenado no app.conf assim


    <configuration>
        <appSettings>
            <add key="ConfiguracaoHorario" value="BLABLABLA" />
            <add key="IP_Servidor" value="BLABLABLA" />
        </appSettings>
    </configuration>


    // E para recuperar do arquivo as informações

comboBox_configuracao_horario.Text = ConfigurationManager.AppSettings.Get("ConfiguracaoHorario");
                txt_servidor_banco_dados.Text = ConfigurationManager.AppSettings.Get("IP_Servidor");

This is the simplest way to do, in your case, in this more complete structure, I suggest serializing a class in this structure, to create your configuration xml.

    
05.10.2015 / 20:51