10

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>
KyleMit
  • 30,350
  • 66
  • 462
  • 664
GibboK
  • 71,848
  • 143
  • 435
  • 658
  • 1
    http://haacked.com/archive/2007/03/11/custom-configuration-sections-in-3-easy-steps.aspx – GibboK Nov 25 '13 at 14:15
  • `ConfigurationManager.AppSettings["MyAppSetting"]` only gives you the setting keyed with the name "MyAppSetting" under `` in your configuration file. – Sameer Singh Nov 25 '13 at 14:15
  • 1
    Check this link http://stackoverflow.com/questions/6329114/how-to-read-a-values-from-new-section-in-web-config – Suraj Singh Nov 25 '13 at 14:19
  • `SingleTagSectionHandler` is **deprecated**, and should use `ConfigurationSection` instead ([link](https://learn.microsoft.com/en-us/dotnet/api/system.configuration.iconfigurationsectionhandler?view=netframework-4.8)) – wangkaibule Feb 16 '22 at 02:18

5 Answers5

24

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.

Community
  • 1
  • 1
Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
3

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"];
Niels R.
  • 7,260
  • 5
  • 32
  • 44
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
1
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.

Sam Leach
  • 12,746
  • 9
  • 45
  • 73
0

You're defining a configuration section instead of a value in AppSettings. You can simply add your setting to AppSettings:

<appSettings>
      ... may be some settings here already
      <add key="server" value="http://example.com" />
</appSettings>

Custom config sections are typically used for more complicated configurations (e.g. multiple values per key, non-string values, etc.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

If you want to get the value from the app settings your appsetting element in configuration file must have a key.

define your sever value as mentioned below under configuration section:

<configuration>
    <appSettings>
          <add key="server" value="http://example.com" />
    </appSettings>
    ...
    ...
    ...
</configuration>

Now execute below code line to get the server url:

string peopleXMLPath = ConfigurationManager.AppSettings["server"].ToString();
SpiderCode
  • 10,062
  • 2
  • 22
  • 42