How to create a new configSections in App.config

Asked

Viewed 407 times

5

I am working with an application that currently uses the section appSettings to obtain 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 don’t know how to create a class to use this type of configuration.

How do I create this configuration class? The way this class is used is from ConfigurationManager.AppSettings also?

1 answer

1

Good afternoon,

I had to store for some time some information about an application, environment settings of the same, it is 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 most complete structure, I suggest serialize a class in this structure, to create its xml, configuration.

  • Okay, my question is to use something more elaborate than the onfigurationManager.AppSettings. How do I do that?

  • This notebook is suitable for you: https://msdn.microsoft.com/pt-br/library/system.configuration.configurationelement(v=vs.110). aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.