Q:

A question about writing data to a custom section?

below is my class thanks to the article found at:
URL: Derik Whittaker
My Code: 
public class FavsSection : ConfigurationSection
    {
        public override bool IsReadOnly()
        {
            return base.IsReadOnly();
        }

        public FavsSection() // default Constructor.
        { }

        [ConfigurationProperty("Items", IsRequired=true)]
        public FavouritesCollection FavsItems
        {
            get 
            {
                return (FavouritesCollection)(base ["Items"]);
            }       
        }
    }

    [ConfigurationCollection(typeof(FavouriteElement))]
    public class FavouritesCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new FavouriteElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((FavouriteElement)(element)).ItemType;
        }

        public FavouriteElement this[int idx]
        {
            get
            {
                return (FavouriteElement)BaseGet(idx);
            }
        }

        public override bool IsReadOnly()
        {
            return base.IsReadOnly();
        }
    }

    public class FavouriteElement : ConfigurationElement
    {
        [ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
        public string ID
        {
            get
            {
                return ((string)(base["id"]));
            }
            set
            {
                base["id"] = value;
            }
        }

        [ConfigurationProperty("path", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string Path
        {
            get
            {
                return ((string)(base["path"]));
            }
            set
            {
                base["path"] = value;
            }
        }
    }

My config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="FavouritesMenu" type="MyFileExplorer.FavsSection, MyFileExplorer" />
  </configSections>
  <FavouritesMenu>
    <Items>
      <add id="1" path="c:\foo" />
      <add id="2" path="C:\foo1" />
    </Items>
  </FavouritesMenu>
</configuration>

As you can see I am trying to write data into my custom section called 'Favourites Menu'. I think I have got the basic gist of the idea but I don;t see how to make my next step ... something got do with the 'IsReadOnly' method? Can someone please help me fill in the blanks? Feel free to rename things to make it easier to read? I thought I would make a half decent effort before I asked for help ...
RESEARCH: StackOverFlow - SAME QUESTION!
---------- Got lost on Pike65's comment ... cannot write to collection because it is set to read only.
I presume the collection needs setting to IsReadOnly false and some helper methods are needed to add data into the collection? This part is all alittle hazy to me ... 
Thanks for reading,
Ibrar

A:

I have been doing some basic testing and to 'NO' surprise ... the above actually works. You just need to make sure that when you want to pass data to your config section, then by default they are read only. So you will need to override the 'isReadOnly()' method inorder for the above code to work.
So the solution is that the above peice of code, does work ... you just need to override an extra method to allow you to access the collection responsible for holding your element data and manipulate its contents via the properties you define in the class that extends or inherits from the Configuration Element class.
UPDATE:
The above code sample I pasted in allows you to edit what already exists in the config file inside your custom section. In order to add a new item for example like the following:
            FavsSection favconfig = (FavsSection)config.GetSection("FavouritesMenu");

            ToolStripMenuItem menu = (ToolStripMenuItem)returnMenuComponents("favouritesToolStripMenuItem", form);

            ToolStripItemCollection items = menu.DropDownItems;

            for (int i = 0; i < items.Count; i++)
            {
                //favconfig.FavsItems[i].ID = i.ToString();
                //favconfig.FavsItems[i].Path = items[i].Text;

                favconfig.FavsItems[i] = new FavouriteElement()
                {
                    ID = i.ToString(),
                    Path = items[i].Text
                };
            }

As you can see above, I am physically adding a new 'FavouriteElement' object into the collection returned by the property 'favconfig.FavItems'. In order to to do this, one property needs extending to support this.
public FavouriteElement this[int idx]
{
    get
    {
        return (FavouriteElement)BaseGet(idx);
    }
    set
    {
      base.BaseAdd(value);
    }
}

This indexer or paramterful property as 'Jeffrey Richter' calls them needs to have it's 'Set' accessor implemented as shown above in the code snippet. I have pasted it in here as it did not take long to figure out and most of the code is changed using a template I have used from Derik Whittaker's Article. Hopefully this will alow other coders to implement something similar.
Another solution would be to simply rather than 'getting' the collection all the time that 'lassoes' together all my 'FavouriteElements', you could implement the 'set' accessor for the related property. I have not tested this but I might be worth trying out.

