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
  • 35,223
  • 60
  • 418
  • 600
GibboK
  • 68,054
  • 134
  • 405
  • 638
  • 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://docs.microsoft.com/en-us/dotnet/api/system.configuration.iconfigurationsectionhandler?view=netframework-4.8)) – wangkaibule Feb 16 '22 at 02:18

5 Answers5

22

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,419
  • 3
  • 33
  • 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,038
  • 5
  • 29
  • 44
CodeCaster
  • 139,522
  • 20
  • 204
  • 252
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,362
  • 8
  • 42
  • 72
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
  • 144,385
  • 11
  • 166
  • 231
-1

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
  • 9,912
  • 1
  • 21
  • 42