簡體   English   中英

ConfigurationManager.GetSection 返回 null

[英]ConfigurationManager.GetSection returns null

這是我的 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;
}

如果您能指出我做錯了什么,我將不勝感激

使用節處理程序對配置文件中的設置進行分組

例如,您可以按照以下內容進行操作

private void ReadSettings()
{
    NameValueCollection loc = 
   (NameValueCollection )ConfigurationSettings.GetConfig("procedureList");
}

MSDN ConfigurationManager.GetConfig 方法

如果您正在測試您的類,您必須將配置復制到您的測試項目中的app.config

使用即時窗口檢查它指向哪個配置文件。 在我的情況下,我有 app.config,我希望它能夠讀取,但是在使用命令時。 ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) 它指向其他類似 nuintrunner.exe.config 的信息,因為該信息已加載到 bin 中。 這有助於加載正確的配置文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM