How to use ConfigurationManager.AppSettings with a custom section?

46,643

Solution 1

I think you need to get the config section, and access that:

var section = ConfigurationManager.GetSection("server") as NameValueCollection;
var value = section["url"];

And you also need to update your config file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <configSections>
    <section name="device" type="System.Configuration.NameValueSectionHandler" />
    <section name="server" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <device>
    <add key="id" value="1" />
    <add key="description" value="petras room" />
    <add key="location" value="" />
    <add key="mall" value="" />
  </device>
  <server>
    <add key="url" value="http://example.com" />
  </server>
</configuration>

Edit: As CodeCaster mentioned in his answer, SingleTagSectionHandler is for internal use only. I think NameValueSectionHandler is the preferred way to define config sections.

Solution 2

The SingleTagSectionHandler documentation says:

This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.

You can retrieve it as a HashTable and access its entries using Configuration.GetSection():

Hashtable serverTag = (Hashtable)ConfigurationManager.GetSection("server");

string serverUrl = (string)serverTag["url"];

Solution 3

string peopleXMLPath = ConfigurationManager.AppSettings["server"];

gets the value from the appSettings part of the app.config file but you are storing your value in

<server url="http://example.com" />

Either put the value in the appSettings section as below or retrieve the value from its current location.

You need to add a key value pair to your config's appSettings section. As below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="server" value="http://example.com" />
    </appSettings>
</configuration>

Your reading code is correct but you should probably check for null. If the code fails to read the config value the string variable will be null.

Share:
46,643
GibboK
Author by

GibboK

A professional and enthusiastic Senior Front End Developer. Listed as top 2 users by reputation in Czech Republic on Stack Overflow. Latest open source projects Animatelo - Porting to JavaScript Web Animations API of Animate.css (430+ stars on GitHub) Industrial UI - Simple, modular UI Components for Shop Floor Applications Frontend Boilerplate - An opinionated boilerplate which helps you build fast, robust, and adaptable single-page application in React Keyframes Tool - Command line tool which convert CSS Animations to JavaScript objects gibbok.coding📧gmail.com

Updated on November 19, 2020

Comments

  • GibboK
    GibboK over 3 years

    I need to get "http://example.com" from using App.config file.

    But at the moment I am using:

    string peopleXMLPath = ConfigurationManager.AppSettings["server"];
    

    I cannot get the value.

    Could you point out what I am doing wrong?

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <configSections>
        <section name="device" type="System.Configuration.SingleTagSectionHandler" />
        <section name="server" type="System.Configuration.SingleTagSectionHandler" />
      </configSections>
      <device id="1" description="petras room" location="" mall="" />
      <server url="http://example.com" />
    </configuration>
    
  • GibboK
    GibboK over 10 years
    using my xml does not work unfortunately,... should i change my xml?
  • Chris Mantle
    Chris Mantle over 10 years
    Yes. You'll need to change url="http://example.com" from being an attribute on server to being a child add tag.