Home
Manage Your Code
Snippet: config: create custom sections (C#)
Title: config: create custom sections Language: C#
Description: define your section in config Views: 347
Author: Jagpreet Singh Date Added: 6/14/2012
Copy Code  
1/// <summary>

2    /// Section Group which contains all the configuration 

3    /// </summary>

4    public class HeartBeatConfigurationSectionGroup : ConfigurationSectionGroup
5    {
6        #region Constants   ...        #endregion
11        #region Private Helper Methods   ...        #endregion
14        #region Public Properties   ...        #endregion
30    }
31
32//====================================================

33
34public class ServicesConfigurationSection : ConfigurationSection
35    {
36        #region Constants   ...        #endregion
41        #region Public Properties   ...        #endregion
110    }
111
112
113//====================================================

114
115[ConfigurationCollection(typeof(AddElement))]
116    public class AddCollection : ConfigurationElementCollection
117    {
118        /// <summary>

119        /// Override the method

120        /// </summary>

121        /// <returns></returns>

122        protected override ConfigurationElement CreateNewElement()
123        {
124            return new AddElement();
125        }
126
127        /// <summary>

128        /// Override the GetElementKey

129        /// </summary>

130        /// <param name="element"></param>

131        /// <returns></returns>

132        protected override object GetElementKey(ConfigurationElement element)
133        {
134            return ((AddElement)(element)).Name;
135        }
136
137        /// <summary>

138        /// Create the indexer

139        /// </summary>

140        /// <param name="idx"></param>

141        /// <returns></returns>

142        public AddElement this[int idx]
143        {
144            get
145            {
146                return (AddElement)BaseGet(idx);
147            }
148        }
149    }
150
151//====================================================

152
153 public class AddElement : ConfigurationElement
154    {      
155        /// <summary>

156        /// Represents the name of the key.

157        /// </summary>

158        [ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = false)]
159        public string Name
160        {
161            get { return ((string)(base["name"])); }
162
163        }
164
165    }
Usage
to create custom section in config file
e.g.