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>
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.
You cannot use multiple configuration files (i.e. one per library project) without coding.
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.
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.
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 .. }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With