C# ConfigurationManager.GetSection 返回 null
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11873151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ConfigurationManager.GetSection returns null
提问by edepperson
Here is my app.config
这是我的 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>
But when I call it in code, I'm getting a null back
但是当我在代码中调用它时,我得到了一个空值
public void samplemethod()
{
NameValueCollection nvc = ConfigurationManager.GetSection("procedureList") as NameValueCollection;
string[] keys = nvc.AllKeys;
}
I would appreciate any help pointing out what I've done wrong
如果您能指出我做错了什么,我将不胜感激
采纳答案by MethodMan
Using section handlers to group settings in the configuration file
For example you can follow something like the following
例如,您可以按照以下内容进行操作
private void ReadSettings()
{
NameValueCollection loc =
(NameValueCollection )ConfigurationSettings.GetConfig("procedureList");
}
回答by David Soler
If you are testing your class you must copy the configuration to the app.configin your Test project.
如果您正在测试您的类,您必须将配置复制到app.config您的测试项目中。

