/// <summary> /// Gets or sets the <see cref="CategorizedSettingsElement"/> object at the specified index. /// </summary> /// <param name="index">Zero-based index for the <see cref="CategorizedSettingsElement"/> object to retrieve.</param> /// <returns>The <see cref="CategorizedSettingsElement"/> object at the specified index if it exists; otherwise null.</returns> public CategorizedSettingsElement this[int index] { get { if (index >= base.Count) { throw new IndexOutOfRangeException(); } CategorizedSettingsElement setting = (CategorizedSettingsElement)base.BaseGet(index); setting.SetCryptoKey(m_cryptoKey); return(setting); } set { if ((object)base.BaseGet(index) != null) { base.BaseRemoveAt(index); } base.BaseAdd(index, value); Modified = true; } }
/// <summary> /// Removes a <see cref="CategorizedSettingsElement"/> object if it exists. /// </summary> /// <param name="setting">The <see cref="CategorizedSettingsElement"/> object to remove.</param> public void Remove(CategorizedSettingsElement setting) { if (base.BaseIndexOf(setting) >= 0) { Remove(setting.Name); Modified = true; } }
/// <summary> /// Adds a new <see cref="CategorizedSettingsElement"/> object if one does not exist. /// </summary> /// <param name="setting">The <see cref="CategorizedSettingsElement"/> object to add.</param> public void Add(CategorizedSettingsElement setting) { if ((object)base.BaseGet(setting.Name) == null) { // Add the element only if it does not exist. setting.Category = this; setting.SetCryptoKey(m_cryptoKey); base.BaseAdd(setting); Modified = true; } }
/// <summary> /// Adds a new <see cref="CategorizedSettingsElement"/> object if one does not exist. /// </summary> /// <param name="name">Name of the <see cref="CategorizedSettingsElement"/> object.</param> /// <param name="value">Value of the <see cref="CategorizedSettingsElement"/> object.</param> /// <param name="description">Description of the <see cref="CategorizedSettingsElement"/> object.</param> /// <param name="encryptValue">true if the Value of <see cref="CategorizedSettingsElement"/> object is to be encrypted; otherwise false.</param> /// <param name="scope">One of the <see cref="SettingScope"/> values.</param> public void Add(string name, object value, string description, bool encryptValue, SettingScope scope) { if ((object)base.BaseGet(name) == null) { // Add the element only if it does not exist. CategorizedSettingsElement setting = new CategorizedSettingsElement(this, name); setting.Update(value, description, encryptValue, scope); Add(setting); } }
/// <summary> /// Gets the <see cref="CategorizedSettingsElement"/> object with the specified name. /// </summary> /// <param name="name">Name of the <see cref="CategorizedSettingsElement"/> object to retrieve.</param> /// <param name="ensureExistance">true if the <see cref="CategorizedSettingsElement"/> object is to be created if it does not exist; otherwise false.</param> /// <returns>The <see cref="CategorizedSettingsElement"/> object with the specified name if it exists; otherwise null.</returns> public CategorizedSettingsElement this[string name, bool ensureExistance] { get { // Add setting since it's not there if (ensureExistance && (object)base.BaseGet(name) == null) { Add(name, string.Empty); } CategorizedSettingsElement setting = (CategorizedSettingsElement)base.BaseGet(name); // Set the crypto key for the setting if ((object)setting != null) { setting.SetCryptoKey(m_cryptoKey); } return(setting); } }
/// <summary> /// Reads XML from the configuration file. /// </summary> /// <param name="reader">The <see cref="System.Xml.XmlReader"/> object, which reads from the configuration file.</param> protected override void DeserializeSection(XmlReader reader) { using (BlockAllocatedMemoryStream configSectionStream = new BlockAllocatedMemoryStream()) { XmlDocument configSection = new XmlDocument(); configSection.Load(reader); configSection.Save(configSectionStream); // Adds all the categories that are under the categorizedSettings section of the configuration file // to the property collection. Again, this is essentially doing what marking a property with the // <ConfigurationProperty()> attribute does. If this is not done, then an exception will be raised // when the category elements are being deserialized. if ((object)configSection.DocumentElement != null) { XmlNodeList categories = configSection.DocumentElement.SelectNodes("*"); if ((object)categories != null) { foreach (XmlNode category in categories) { ConfigurationProperty configProperty = new ConfigurationProperty(category.Name, typeof(CategorizedSettingsElementCollection)); base.Properties.Add(configProperty); if ((object)m_sections != null) { CategorizedSettingsElementCollection settingsCategory = new CategorizedSettingsElementCollection { Name = category.Name, Section = this, }; settingsCategory.SetCryptoKey(m_cryptoKey); m_sections.Add(category.Name, settingsCategory); // Read all elements within this category section XmlNodeList elements = category.SelectNodes("*"); SettingScope scope; if ((object)elements != null) { foreach (XmlNode element in elements) { CategorizedSettingsElement categorySetting = new CategorizedSettingsElement(settingsCategory); categorySetting.Name = element.GetAttributeValue("name"); categorySetting.Value = element.GetAttributeValue("value"); categorySetting.Description = element.GetAttributeValue("description") ?? ""; categorySetting.Encrypted = element.GetAttributeValue("encrypted").ToNonNullNorWhiteSpace("false").ParseBoolean(); if (Enum.TryParse(element.GetAttributeValue("scope").ToNonNullNorWhiteSpace("Application"), out scope)) { categorySetting.Scope = scope; } else { categorySetting.Scope = SettingScope.Application; } settingsCategory.Add(categorySetting); } } } } } } m_sectionLoaded = true; if ((object)m_sections == null) { configSectionStream.Seek(0, SeekOrigin.Begin); base.DeserializeSection(XmlReader.Create(configSectionStream)); } } }
/// <summary> /// Gets the index of the specified <see cref="CategorizedSettingsElement"/> object. /// </summary> /// <param name="setting">The <see cref="CategorizedSettingsElement"/> object whose index is to be retrieved.</param> /// <returns>Index of the specified <see cref="CategorizedSettingsElement"/> object if found; otherwise -1.</returns> public int IndexOf(CategorizedSettingsElement setting) { return(base.BaseIndexOf(setting)); }