2

I want to add the custom configsection in the 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 it is referring to Projects section.

Can anybody tell me way to do it? And how to access it in my code?

@Bhaskar: Please find the code for your comment.

 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; } }
    } 
Geeta
  • 69
  • 1
  • 8
  • Consider putting these settings in a separate XML file instead of in the app.config. – Sjoerd Sep 20 '11 at 12:27
  • @Sjoerd , what is acheived by putting them in separate config files ? The entries should all be in one file as they are related. – Bhaskar Sep 20 '11 at 12:41
  • @Geeta , In your code above I see this - `ConfigurationManager.GetSection("RegisterCompanies")` - where is the section `RegisterCompanies` declared in your config file ? I can see only `
    – Bhaskar Sep 21 '11 at 12:53

3 Answers3

3

You should check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

Highly recommended, well written and extremely helpful! I've learned how to deal with custom config sections from those excellent articles.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

I base all my configuration management code on the classes I collected here. This is an example, and here's some documentation. Note that this is code I personally refactored from a blog post that isn't available on-line any more.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
  • i have gone through your code.But you hav defined your own class to handle the collection like ConfigElementCollectionBase.However i want to do it using configuration collection element.If you know can you tell me how to write the code for the above section using ConfigurationElementCollection. – Geeta Sep 21 '11 at 03:51
-1

This will help you http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.80).aspx

Bhaskar
  • 7,443
  • 5
  • 39
  • 51
  • I just tried one simple example which is mentioned in one website.whenever i do ConfigurationManager.GetSection("Companies") its returning null.Can anybody tell me what is the exact problem. – Geeta Sep 21 '11 at 04:11
  • @Geeta , you need to show your config file for me to point out the exact problem. – Bhaskar Sep 21 '11 at 08:10
  • Please find the config file content
    – Geeta Sep 21 '11 at 08:25
  • have you defined your handler type "CustomConfigSectionTester.RegisterCompaniesConfig,CustomConfigSectionTester" ? If this is not defined , the corresponding section may not be loaded. – Bhaskar Sep 21 '11 at 08:59
  • yes i have added RegisterCompaniesConfig class which is derived from ConfigSection.I also implemented ConfigurationElementCollection and ConfigurationElement. – Geeta Sep 21 '11 at 10:44
  • Are you sure its `ConfigSection` you derive from ? This seems to be incorrect if I remember correctly - it should be `ConfigurationSection` I suppose. Without the code it will be difficult to guess but its obvious that your ConfigurationManager is unable to locate a scetion by name "Companies". – Bhaskar Sep 21 '11 at 11:02