How can I get all the properties from a ConfigSection in C#

178 views Asked by At

(after all, sorry for my english) I need to ask you how can I get the key value pair from a ConfigSection in c#. I´m creating a DLL which take the config file from the executable, from this configfile I need a particular SectionGroup called "Process" which will have all the process that the system can call, so the sections names will represent the valid Arguments for the input.

//Code to get Config and sectionGroup

 _argumentosValidos = new List<ConfigurationSection>();
            try
            {
                //Crear copia en memoria de archivo de configuración del ejecutable.
                Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                //Obtiene las section válidas, que son aquellas que forman parte del SectionGroup process:
                ConfigurationSectionGroup sectionGroup = configuration.GetSectionGroup("Process");

                //Obtener las sections válidas del sectionGroup
                ConfigurationSectionCollection sections = sectionGroup.Sections;

                //Listar los argumentos válidos
                foreach (ConfigurationSection section in sections)
                {
                    _argumentosValidos.Add(section);
                }
            }
            catch (NullReferenceException e)
            {
                throw new ArgumentValidatorConfigurationException(e.Message, e.StackTrace);
            }
            catch (ConfigurationException e)
            {

                throw new ArgumentValidatorConfigurationException(e.Message, e.StackTrace);
            }
            catch (Exception e)
            {
                throw new ArgumentValidatorConfigurationException(e.Message, e.StackTrace);
            }
        }

Now, I need to get all the properties from each ConfigSection, and this is where I can´t figure out how to do it, can you help me?

PD: I could only get the name.

foreach(ConfigurationSection section in _argumentosValidos)
            {
                _validArgument.ArgumentName = section.SectionInformation.Name; 
               
            }

And this is the configFile

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name ="Process">
      <section name="Posmonac" type="System.Configuration.NameValueSectionHandler"/>
      <section name="Posmonex" type="System.Configuration.NameValueSectionHandler"/>
      <section name="PZPosDomest" type="System.Configuration.NameValueSectionHandler"/>
    </sectionGroup>
    <Process>
      <Posmonac>
        <add key="Posdiaria" value ="DWDD004_PROCESAR"/>
      </Posmonac>
      <Posmonex>
        <add key ="POSMONEX" value ="DWDD007_PROCESAR"/>
      </Posmonex>
      <PzPosDomest>
        <add key ="PZPOSDOMEST" value ="PZ_PROCESAR"/>
      </PzPosDomest>
    </Process>
  </configSections>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
    </startup>
</configuration>

Thank you.

0

There are 0 answers