views:

32

answers:

1

Hi guys, 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: 
IbrarMumtaz