How to use ConfigurationManager.AppSettings with a

2019-01-26 09:23发布

问题:

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>

回答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.



回答2:

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"];


回答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.



回答4:

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.



回答5:

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();