配置选择组获取选择值

本文关键字:选择 获取 配置 | 更新日期: 2023-09-27 18:15:09

我有一些问题与获取配置信息,如何获得isUpdatable值的一些值test1或test2,或test3如果参数将得到外面。

我在配置中有这个示例。

<configSections>
<sectionGroup name ="UpdateSettings">
  <section name="test1" type="System.Configuration.NameValueSectionHandler"/>
  <section name="test2" type="System.Configuration.NameValueSectionHandler"/>
  <section name="test3" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup>
</configSections>
<UpdateSettings>
<test1>
  <add key="isUpdatable" value="0"/> <!-- get from service - 1, get from config - 0,-->
</test1>
<test2>
  <add key="isUpdatable" value="1"/>
</test2>
<test3>
  <add key="isUpdatable" value="1"/>
</test3>
</UpdateSettings>

和这个宁静的c#代码

  name = "test1";
  Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  ConfigurationSectionGroup UpdateSettings = config.GetSectionGroup("UpdateSettings");
  /* this is Connect beetwen selection group and selection, but how?*/        
  NameValueCollection sectionSettings = ConfigurationManager.GetSection(name) as NameValueCollection;
  var value = sectionSettings["isUpdatable"];

配置选择组获取选择值

你非常接近,你有一个值数组,所以sectionSettings[0]将获得第一个值,sectionSettings[1]将获得第二个值,以此类推。

  name = "test1";
  Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  ConfigurationSectionGroup UpdateSettings = config.GetSectionGroup("UpdateSettings");        
  NameValueCollection sectionSettings = ConfigurationManager.GetSection(name) as NameValueCollection;
  var value = sectionSettings[0]["isUpdatable"];