如何使用ConfigurationManager.AppSettings与自定义部分?

10

我需要从App.config文件中获取"http://example.com"。

但目前我正在使用:

string peopleXMLPath = ConfigurationManager.AppSettings["server"];

我无法获取该值。

你能指出我做错了什么吗?

<?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
http://haacked.com/archive/2007/03/11/custom-configuration-sections-in-3-easy-steps.aspx - GibboK
ConfigurationManager.AppSettings["MyAppSetting"] 只会返回你在配置文件的 <appSettings> 下以 "MyAppSetting" 为键名的设置。 - Sameer Singh
1
请查看此链接:https://dev59.com/52w15IYBdhLWcg3w6f40 - Suraj Singh
SingleTagSectionHandler已经弃用,应该使用ConfigurationSection代替(链接 - wangkaibule
5个回答

24

我认为你需要获取配置部分,并访问它:

var section = ConfigurationManager.GetSection("server") as NameValueCollection;
var value = section["url"];

你还需要更新你的配置文件:

<?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>

编辑:正如CodeCaster在他的答案中提到的SingleTagSectionHandler仅供内部使用。 我认为NameValueSectionHandler是定义配置部分的首选方法。


使用我的 XML 不起作用,不幸的是... 我应该更改我的 XML 吗? - GibboK
1
是的,您需要将 url="http://example.com" 从作为 server 属性更改为作为子级 add 标记。 - Chris Mantle

4
SingleTagSectionHandler文档中指出:
此API支持.NET Framework基础架构,不建议直接在您的代码中使用。
您可以将其作为HashTable检索,并使用Configuration.GetSection()访问其条目:
Hashtable serverTag = (Hashtable)ConfigurationManager.GetSection("server");

string serverUrl = (string)serverTag["url"];

1
string peopleXMLPath = ConfigurationManager.AppSettings["server"];

从 app.config 文件的 appSettings 部分获取值,但您将值存储在其中。
<server url="http://example.com" />

要么按照以下方式将值放入“appSettings”部分,要么从其当前位置检索该值。
您需要向配置的“appSettings”部分添加键值对。如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="server" value="http://example.com" />
    </appSettings>
</configuration>

你的读取代码是正确的,但是你可能需要检查是否为 null。如果代码无法读取配置值,则 string 变量将为 null。

0
你正在定义一个配置“部分”而不是在“AppSettings”中定义一个“值”。你可以简单地将设置添加到“AppSettings”中:
<appSettings>
      ... may be some settings here already
      <add key="server" value="http://example.com" />
</appSettings>

自定义配置节通常用于更复杂的配置(例如每个键多个值,非字符串值等)。


0
如果您想从应用程序设置中获取值,则配置文件中的appsetting元素必须具有键。
请按照以下所述在configuration部分定义您的服务器值:
<configuration>
    <appSettings>
          <add key="server" value="http://example.com" />
    </appSettings>
    ...
    ...
    ...
</configuration>

现在执行下面的代码行以获取服务器URL:

string peopleXMLPath = ConfigurationManager.AppSettings["server"].ToString();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接