-1

Trying to loop through app.config file, searched the posts but haven't had luck

app.config

 <configSections>
    <section name="US" type="System.Configuration.NameValueSectionHandler"/>
    <section name="UK" type="System.Configuration.NameValueSectionHandler"/>
 </configSections>

  <US>
    <add key="UserName" value="test" />
    <add key="Password" value="test" />
    <add key="baseURI" value="http://test.com />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </US>

  <UK>
   <add key="UserName" value="test1 />
    <add key="Password" value="test1 />
    <add key="baseURI" value="http://test1.com />
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </UK>

I need to loop through app.config file and get the key value data like the below for US and UK sections.

*Result:*

Section : US

Username: test
Password: test
baseURI: http://test.com

Section : UK

Username: test1
Password: test1
baseURI: http://test1.com
josh
  • 1
  • 1
  • Possible duplicate of [How to get the values of a ConfigurationSection of type NameValueSectionHandler](https://stackoverflow.com/questions/3461418/how-to-get-the-values-of-a-configurationsection-of-type-namevaluesectionhandler) – Daniel B May 29 '18 at 21:12
  • Try using an AppSettingsSection instead of a NameValueCollection. Something like this: https://stackoverflow.com/a/14384706/336511 – Daniel B May 29 '18 at 21:15
  • I tried the way in the samples, but I get the error ConfigurationErrorsException was unhandled (An unhandled exception of type "System.Configurartion.ConfigurationErrorsException" occurred in System.Configuration.dll C# var section = (AppSettingsSection) ConfigurationManager.GetSection("USMailSettings"); -- error configuration error exception string results = section.Settings["UserName"].Value; – josh May 31 '18 at 20:06
  • there's no USMailSettings config in ys sample! use `(AppSettingsSection) ConfigurationManager.GetSection("US");` – Daniel B May 31 '18 at 22:31
  • I tried it but get the same ConfigurationErrorsException – josh Jun 04 '18 at 18:09
  • did u try my answer? @josh – Daniel B Jun 05 '18 at 13:39

1 Answers1

0

There are some problem with yr config file, you are missing some quotes("), plz correct it first.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <!--This MUST be the first node-->
  <configSections>
    <section name="US" type="System.Configuration.AppSettingsSection"/>
    <section name="UK" type="System.Configuration.AppSettingsSection"/>
  </configSections>

  <US>
    <add key="UserName" value="test" />
    <add key="Password" value="test" />
    <add key="baseURI" value="http://test.com" /> <!--misssing a " here!! -->
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </US>

  <UK>
    <add key="UserName" value="test1" /><!--misssing a " here!! -->
    <add key="Password" value="test1" /><!--misssing a " here!! -->
    <add key="baseURI" value="http://test1.com" /><!--misssing a " here!! -->
    <add key="ClientSettingsProvider.ServiceUri" value="" />
  </UK>        

</configuration>

and then in yr code just use ConfigurationManager.OpenExeConfiguration to get the configuration object:

var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var section = (AppSettingsSection)config.GetSection("UK");

var results = section.Settings["UserName"].Value;

Console.WriteLine(results);
Daniel B
  • 3,109
  • 2
  • 33
  • 42