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

I want to use the configuration of my application to store settings for 2 companies, and I would prefer if you could use the section to separate data for one from the other, and then give them different key names.

I check online, but I seem to be a little overwhelmed when people use partitions or find outdated simple ways to use them. can someone pass me a beginner's guide?

The following is an example of my app.config:

<configSections> <section name="FBI" type="" /> <section name="FSCS" type="" /> </configSections> <FSCS> <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/> </FSCS> <FBI> <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/> </FBI> 

Update:

Anwer based advanced solution. in case someone wants to find out.

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:\testfiles\ProccesFolder"/> </FSCS> <FBI> <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/> </FBI> </FileCheckerConfigGroup> </configuration> 

the 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"; } } } 
+50
c # configuration app-config configurationsection
Jan 12 2018-11-11T00:
source share
2 answers
 <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"]; 
+71
Jan 12 '11 at 15:44
source share

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> 

Usage example

 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"] } 
+8
May 23 '14 at 18:59
source share



All Articles