配置选择组获取选择值

时间:2014-07-28 13:11:16

标签: c#

我在获取配置信息方面存在一些问题,如何获得某些值test1或test2的isUpdatable值,或者如果参与者将到达外部则获得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"];

1 个答案:

答案 0 :(得分:3)

你非常接近,你有一系列的数值,所以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"];