Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
501 views
in Technique[技术] by (71.8m points)

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

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 giving them diffrent key names.

I have been checking online but i seem to get a bit overwhelmed when people use sections or find outdated easy ways to use them. could anyone pass me a beginner guide on them?

Below is an example of what my app.config would look like:

  <configSections>
    <section name="FBI" type="" />
    <section name="FSCS" type="" />
  </configSections>

  <FSCS>
    <add key="processingDirectory" value="C:estfilesProccesFolder"/>
  </FSCS>
  <FBI>
    <add key="processingDirectory" value="C:estfilesProccesFolder"/>
  </FBI>

Update:

Advanced solution based on the anwer. in case anyone wanted to know.

App.config:

<configuration>
    <configSections>
        <sectionGroup name="FileCheckerConfigGroup">
          <section name="FBI" type="System.Configuration.NameValueSectionHandler" />
          <section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
        </sectionGroup>
    </configSections>
    <FileCheckerConfigGroup>
        <FSCS>
            <add key="processingDirectory" value="C:estfilesProccesFolder"/>
        </FSCS>
        <FBI>
            <add key="processingDirectory" value="C:estfilesProccesFolder"/>
        </FBI>
    </FileCheckerConfigGroup>
</configuration>

Code:

// Get the application configuration file. 
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Get the collection of the section groups. 
ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;

foreach (ConfigurationSectionGroup sectionGroup in sectionGroups)
{
    if (sectionGroup.Name == "FileCheckerConfigGroup")
    {
        foreach (ConfigurationSection configurationSection in sectionGroup.Sections)
        {
          var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;
          inputDirectory = section["inputDirectory"]; //"C:\testfiles";
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<configSections>
  <section name="FBI" type="System.Configuration.NameValueSectionHandler" />
  <section name="FSCS" type="System.Configuration.NameValueSectionHandler" />
</configSections>

<FSCS>
  <add key="processingDirectory" value="C:estfilesProccesFolder"/>
</FSCS>
<FBI>
  <add key="processingDirectory" value="C:estfilesProccesFolder"/>
</FBI>

And then:

var section = ConfigurationManager.GetSection("FSCS") as NameValueCollection;
var value = section["processingDirectory"];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...