Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom config section in app.config? [duplicate]

Tags:

c#

app-config

I want to add a custom configuration section in my app.config file. Is there a way to do it and how can I access these settings in my program. Following is the config section I want to add to my app.config:

<RegisterCompanies>     <Companies>       <Company name="Tata Motors" code="Tata"/>       <Company name="Honda Motors" code="Honda"/>     </Companies> </RegisterCompanies> 
like image 412
Embedd_0913 Avatar asked Aug 22 '09 14:08

Embedd_0913


People also ask

How do I add a config section in app config?

Open the App. config file and add the configSections, sectionGroup and section to it. We need to specify the name and fully qualified type of all the section and section group.

Can we have multiple app config?

You cannot use multiple configuration files (i.e. one per library project) without coding.

Is app config the same as web config?

Web. Config is used for asp.net web projects / web services. App. Config is used for Windows Forms, Windows Services, Console Apps and WPF applications.

What is configSections in web config?

The root Web. config file includes settings that apply to all of the ASP.NET applications that run a specific version of the . NET Framework. Because each ASP.NET application inherits default configuration settings from the root Web.


1 Answers

Import namespace :

using System.Configuration; 

Create ConfigurationElement Company :

public class Company : ConfigurationElement {          [ConfigurationProperty("name", IsRequired = true)]         public string Name         {             get             {                 return this["name"] as string;             }         }             [ConfigurationProperty("code", IsRequired = true)]         public string Code         {             get             {                 return this["code"] as string;             }         } } 

ConfigurationElementCollection:

public class Companies         : ConfigurationElementCollection     {         public Company this[int index]         {             get             {                 return base.BaseGet(index) as Company ;             }             set             {                 if (base.BaseGet(index) != null)                 {                     base.BaseRemoveAt(index);                 }                 this.BaseAdd(index, value);             }         }         public new Company this[string responseString]        {             get { return (Company) BaseGet(responseString); }             set             {                 if(BaseGet(responseString) != null)                 {                     BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));                 }                 BaseAdd(value);             }         }          protected override System.Configuration.ConfigurationElement CreateNewElement()         {             return new Company();         }          protected override object GetElementKey(System.Configuration.ConfigurationElement element)         {             return ((Company)element).Name;         }     } 

and ConfigurationSection:

public class RegisterCompaniesConfig         : ConfigurationSection     {          public static RegisterCompaniesConfig GetConfig()         {             return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies") ?? new RegisterCompaniesConfig();         }          [System.Configuration.ConfigurationProperty("Companies")]             [ConfigurationCollection(typeof(Companies), AddItemName = "Company")]         public Companies Companies         {             get             {                 object o = this["Companies"];                 return o as Companies ;             }         }      } 

and you must also register your new configuration section in web.config (app.config):

<configuration>            <configSections>           <section name="Companies" type="blablaNameSpace.RegisterCompaniesConfig, blablaAssemblyName" ..> 

then you load your config with

var config = RegisterCompaniesConfig.GetConfig(); foreach(var item in config.Companies) {    do something .. } 
like image 103
Jan Remunda Avatar answered Oct 12 '22 23:10

Jan Remunda