2013-11-25 36 views
6

我需要从使用App.config文件中获得“http://example.com”。如何将ConfigurationManager.AppSettings与自定义节一起使用?

但目前我使用:

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> 
+0

http://haacked.com/archive/2007/03/11/custom-configuration-sections -in-3-easy-steps.aspx – GibboK

+0

'ConfigurationManager.AppSettings [“MyAppSetting”]'只提供在配置文件中以''下的名称“MyAppSetting”为键名的设置。 –

+1

检查此链接http://stackoverflow.com/questions/6329114/how-to-read-a-values-from-new-section-in-web-config –

回答

14

我认为你需要得到的配置节,并访问:

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> 

编辑:As CodeCaster mentioned in his answer,SingleTagSectionHandler仅供内部使用。我认为NameValueSectionHandler是定义配置节的首选方式。

+0

使用我的XML不工作不幸,...应该我改变我的XML? – GibboK

+1

是的。您需要将'url =“http://example.com”'从'server'上的属性更改为子'add'标签。 –

+0

非常感谢您的编辑 – GibboK

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

您的阅读代码是正确的,但您应该检查为空。如果代码未能读取配置值,则string变量将为空。

0

你定义配置部分,而不是在一个AppSettings价值。您只需将设置添加到AppSettings

<appSettings> 
     ... may be some settings here already 
     <add key="server" value="http://example.com" /> 
</appSettings> 

Custom config sections通常用于更复杂的配置(如每个键,非字符串值多值等

-1

如果您想从应用程序设置中获取值,则配置文件中的appsetting元素必须具有密钥。

定义为下配置下面提到部分的服务器值:

<configuration> 
    <appSettings> 
      <add key="server" value="http://example.com" /> 
    </appSettings> 
    ... 
    ... 
    ... 
</configuration> 

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

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

SingleTagSectionHandler documentation says

该API支持.NET Framewor k基础结构,并不打算直接在您的代码中使用。

如图所示here不过,你可以检索它作为一个HashTable和访问其元素:

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

string serverUrl = (string)serverTag["url"]; 
相关问题