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>
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:
Now execute below code line to get the server url:
I think you need to get the config section, and access that:
And you also need to update your config file:
Edit: As CodeCaster mentioned in his answer,
SingleTagSectionHandler
is for internal use only. I thinkNameValueSectionHandler
is the preferred way to define config sections.gets the value from the
appSettings
part of the app.config file but you are storing your value inEither 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:
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.You're defining a configuration section instead of a value in
AppSettings
. You can simply add your setting toAppSettings
:Custom config sections are typically used for more complicated configurations (e.g. multiple values per key, non-string values, etc.
The
SingleTagSectionHandler
documentation says:As shown here though, you can retrieve it as a
HashTable
and access its entries: