How to use ConfigurationManager.AppSettings with a custom section?

感情迁移 提交于 2019-11-29 10:19:27
Chris Mantle

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.

The SingleTagSectionHandler documentation says:

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

As shown here though, you can retrieve it as a HashTable and access its entries:

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

string serverUrl = (string)serverTag["url"];
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.

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.

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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!