How to create a custom configuration section in app.config

I want to add custom configuration to app.config file as follows

<Companies> <Company name="" code=""/> <Company name="" code=""/> </Companies> <Employees> <Employee name="" Des="" addr="" sal=""/> <Employee name="" Des="" addr="" sal=""/> </Employeess> <Departments> <Department Id="" Projects=""/> </Departments> <Projects> <Project Path=""/> </Projects> 

In the Department section, this refers to the Projects section.

Can someone tell me a way to do this? And how to access it in my code?

@Bhaskar: find the code for comments.

  public class RegisterCompaniesConfig : ConfigurationSection { public static RegisterCompaniesConfig GetConfig() { return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies")?? new RegisterCompaniesConfig(); } [System.Configuration.ConfigurationProperty("Companies")] public Companies Companies { get { object o = this["Companies"]; return o as Companies; } } } 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); } } protected override System.Configuration.ConfigurationElement CreateNewElement() { return new Company(); } protected override object GetElementKey(System.Configuration.ConfigurationElement element) { return ((Company)element).Name; } } 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; } } } 
+2
app-config
source share
3 answers

You should check out the three-part series of Jon Rista for the .NET 2.0 configuration on CodeProject.

Highly recommended, well written and very helpful! I learned how to handle custom configuration sections from these great articles.

+3
source share

I base all of the configuration management code on the classes that I have compiled here . This is an example , and here is some documentation . Please note that this is a code that I personally reorganized from a blog post that is no longer available on the Internet.

0
source share
-one
source share

All Articles