c# – ConfigurationManager.GetSection返回null

前端之家收集整理的这篇文章主要介绍了c# – ConfigurationManager.GetSection返回null前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的app.config
  1. <configuration>
  2. <configSections>
  3. <section name="procedureList" type="System.Configuration.NameValueSectionHandler,System,Version=4.0.30319,Culture=neutral,PublicKeyToken=b77a5c561934e089"/>
  4. </configSections>
  5.  
  6. <procedureList>
  7. <add key="NAS.spBusObjGetLineProd" value="@area='Melt Shop';@endDt=?date?;@dayonly=1;@obj='Melt Shop Business Objective" />
  8. <add key="NAS.spBusObjGetLineProd" value="@area='Cold Mill';@endDt=?date?;@dayonly=1;@obj='Cold Mill Business Objective" />
  9. </procedureList>
  10. <appSettings>
  11. <add key="Connstr" value=""/>
  12. <add key="Userid" value=""/>
  13. <add key="Timeout" value=""/>
  14. </appSettings>
  15.  
  16. </configuration>

但是当我在代码调用它时,我得到一个null

  1. public void samplemethod()
  2. {
  3. NameValueCollection nvc = ConfigurationManager.GetSection("procedureList") as NameValueCollection;
  4. string[] keys = nvc.AllKeys;
  5. }

我会感谢任何帮助指出我做错了什么

解决方法

Using section handlers to group settings in the configuration file

例如,您可以按照以下内容进行操作

  1. private void ReadSettings()
  2. {
  3. NameValueCollection loc =
  4. (NameValueCollection )ConfigurationSettings.GetConfig("procedureList");
  5. }

MSDN ConfigurationManager.GetConfig Method

猜你在找的C#相关文章