【发布时间】:2012-08-06 01:44:38
【问题描述】:
这是我的 app.config
<configuration>
<configSections>
<section name="procedureList" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.30319, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<procedureList>
<add key="NAS.spBusObjGetLineProd" value="@area='Melt Shop';@endDt=?date?;@dayonly=1;@obj='Melt Shop Business Objective" />
<add key="NAS.spBusObjGetLineProd" value="@area='Cold Mill';@endDt=?date?;@dayonly=1;@obj='Cold Mill Business Objective" />
</procedureList>
<appSettings>
<add key="Connstr" value=""/>
<add key="Userid" value=""/>
<add key="Timeout" value=""/>
</appSettings>
</configuration>
但是当我在代码中调用它时,我得到一个空值
public void samplemethod()
{
NameValueCollection nvc = ConfigurationManager.GetSection("procedureList") as NameValueCollection;
string[] keys = nvc.AllKeys;
}
如果有任何帮助指出我做错了什么,我将不胜感激
【问题讨论】:
-
你投错了。我将在下面发布一个示例以及可以用作参考的链接
-
如果
as转换操作失败,则返回空值。但是,配置文件指出节类型是NameValueSectionHandler——在调用 GetSection() 时应该强制转换为这种类型。 -
即使我将类型更改为 NameValueSectionHandler,我仍然得到一个空返回
-
放置断点时nvc的值是多少..?
-
只是想看看会发生什么,我刚刚返回了一个对象,如下所示,但仍然为空。 object nvc = ConfigurationManager.GetSection("procedureList");
标签: c# app-config