ConfigurationManager.GetSection返回null

ede*_*son 13 c# 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>
Run Code Online (Sandbox Code Playgroud)

但是当我在代码中调用它时,我得到了null

public void samplemethod()
{
    NameValueCollection nvc = ConfigurationManager.GetSection("procedureList") as NameValueCollection;
    string[] keys = nvc.AllKeys;
}
Run Code Online (Sandbox Code Playgroud)

我很感激任何帮助指出我做错了什么

Met*_*Man 6

使用节处理程序对配置文件中的设置进行分组

例如,您可以执行以下操作

private void ReadSettings()
{
    NameValueCollection loc = 
   (NameValueCollection )ConfigurationSettings.GetConfig("procedureList");
}
Run Code Online (Sandbox Code Playgroud)

MSDN ConfigurationManager.GetConfig方法

  • 弄清楚了.我在dll中有app配置,而不是调用表单.因为最终dll将由服务调用,我需要使用ConfigurationManager.OpenExeConfiguration来修复它.谢谢你的推动. (6认同)

Dav*_*ler 5

如果您正在测试您的类,您必须将配置复制到app.config您的测试项目中。