How do you use sections in c# 4.0 app.config?

后端 未结 2 1170
遥遥无期
遥遥无期 2020-12-07 10:58

I want to use my app config to store the settings for 2 companys, and i\'d prefer if it was possible to use a section to seperate the data for one from the other rather then

相关标签:
2条回答
  • 2020-12-07 11:59
    <configSections>
      <section name="FBI" type="System.Configuration.NameValueSectionHandler" />
      <section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
    </configSections>
    
    <FSCS>
      <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
    </FSCS>
    <FBI>
      <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
    </FBI>
    

    And then:

    var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection;
    var value = section["processingDirectory"];
    
    0 讨论(0)
  • 2020-12-07 11:59

    App.config

    <configSections>
      <sectionGroup name="FileCheckers">
        <section name="FBI" type="System.Configuration.NameValueSectionHandler" />
        <section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
      </sectionGroup>
    </configSections>
    
    <FileCheckers>
      <FSCS>
        <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
      </FSCS>
      <FBI>
        <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>
      </FBI>
    </FileCheckers>
    

    Example Usage

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConfigurationSectionGroup fileCheckersGroup = config.SectionGroups["FileCheckers"];
    foreach (ConfigurationSection section in fileCheckersGroup.Sections)
    {
        NameValueCollection sectionSettings = ConfigurationManager.GetSection(section.SectionInformation.SectionName) as NameValueCollection;
        var value = sectionSettings["processingDirectory"]
    }
    
    0 讨论(0)
提交回复
热议问题