Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\testfiles\ProccesFolder"/>   </FSCS>   <FBI>     <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>   </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:\testfiles\ProccesFolder"/>         </FSCS>         <FBI>             <add key="processingDirectory" value="C:\testfiles\ProccesFolder"/>         </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";         }     } } 
like image 783
Andy Avatar asked Jan 12 '11 15:01

Andy


People also ask

What are sections in C?

The link and definition sections are called as preprocessor directives. It gives instructions to the compiler to link function from the system library. For example, the definition section defines all the symbolic constants. #include<stdio.h> For example, #define PI 3.1415.

What are the sections in the C program give example for it?

Link section − In this section, header files that are required to execute the program are included. Definition section − Here, variables are defined and initialised. Global declaration section − In this section, global variables are defined which can be used throughout the program.

How many sections are there in C programming?

A C program is divided into different sections. There are six main sections to a basic c program. The six sections are, Documentation.

What is Section in programming?

When referring to HTML, a section is an area in the web page containing specific programming, text, or images that are separated from other areas using certain HTML tags.


1 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"]; 
like image 104
Darin Dimitrov Avatar answered Sep 20 '22 14:09

Darin Dimitrov