1

I've attempted my first go round with a custom configuration in app.settings. When I try to call the config I get "Unrecognized configuration section servers". What am I doing wrong?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <sectionGroup name="servers" type="System.Configuration.NameValueSectionHandler"></sectionGroup>
      <sectionGroup name="services" type="System.Configuration.NameValueSectionHandler"></sectionGroup>
      <!--<section name="servers.myServers" type="System.Configuration.NameValueFileSectionHandler" />-->
      <!--<section name="services.myServices" type="System.Configuration.NameValueFileSectionHandler" />-->
      <section name="ServiceMonitor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <servers>
    <myServers>
      <add key="server1" value="myserverhost"/>
    </myServers>
  </servers>

  <services>
    <myServices>
      <add key="service1" value="spooler"/>
      <add key="service2" value="Apple Mobile Device"/>
    </myServices>
  </services>

  <userSettings>
    <ServiceMonitor.Properties.Settings>
      <setting name="service1" serializeAs="String">
        <value>spooler</value>
      </setting>
      <setting name="service2" serializeAs="String">
        <value>Apple Mobile Device</value>
      </setting>
    </ServiceMonitor.Properties.Settings>
  </userSettings>
  <appSettings>
    <add key="service1" value="spooler"/>
    <add key="service2" value="Apple Mobile Device"/>
  </appSettings>

</configuration>

Here's the C# code I'm using to call the config.

NameValueCollection settings = ConfigurationManager.GetSection("servers/myServers") as NameValueCollection;
if (settings != null)
{
    foreach (string key in settings)
    {
        Console.WriteLine("{0} : {1}", key, settings[key]);
    }
}
tshepang
  • 12,111
  • 21
  • 91
  • 136
Dovey
  • 43
  • 1
  • 6
  • 4
    If your file is named "app.settings", then that explains why it's not working. You want it to be named "app.config". – John Saunders Sep 04 '11 at 01:58
  • I note that you don't have a fully qualified name for the class; that might be the problem (not sure though, but I'd put it in). – Noon Silk Sep 04 '11 at 01:59

1 Answers1

1

I think you need to add the section element (which is commented out in your example):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="services">
      <section name="myServices" 
        type="System.Configuration.NameValueSectionHandler" />            
    </sectionGroup>
  </configSections>
</configuration>

See sectionGroup Element for configSections for an example.

svick
  • 236,525
  • 50
  • 385
  • 514
Randy Levy
  • 22,566
  • 4
  • 68
  • 94
  • it was getting hung up on my usersettings section. I didn't need it anyway so I stripped it out and it's working now. – Dovey Sep 04 '11 at 10:56
  • @Dovey, you should answer your question with the solution and accept that answer. – Randy Levy Sep 04 '11 at 14:58