Click here to Skip to main content
15,896,730 members
Articles / Programming Languages / C#

Class Library to Automatically Maintain Persistent Variables, Properties and Data Between Sessions

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
11 Jan 2008CPOL15 min read 44.1K   594   43  
A class library that maintains persistent variables, properties and data between sessions in XML files. It supports default values for new variables and events when the values in the XML file are changed
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Xml;

// Part of RJConfig V1.3

// Classes for the Config Section. A section contains Items which in turn contains Variables.
namespace RJConfig
{
	/// <summary>
	/// Delegate signature for the ConfigFileSectionChanged event. This event is raised when FileWatchEnabled is
	/// set to true and some external source has changed an element in the Section where the event is subscribed.
	/// </summary>
	/// <param name="sender">The Section instance where there has been a change.</param>
	/// <param name="e">CfgEventArgs</param>
	public delegate void ConfigFileSectionChanged (object sender, CfgEventArgs e);
	/// <remarks>
	/// This class represents one Section in the configuration/properties file.
	/// A Section have one or more Items in it. The Section is derived from the ItemDict class which
	/// means that it actually holds a dictionary of Items.
	/// </remarks>
	public class Section : ItemDict
	{
		/// <summary>
		/// Event list for ConfigFileSectionChanged event. This event is raised when Variables in the Items of
		/// this Section is different from the Variables in the file after the file has been changed.
		/// FileWatchEnabled must be set to true for this to happen.
		/// </summary>
		public event ConfigFileSectionChanged OnConfigFileSectionChanged;
		/// <summary>
		/// Internal member for name of the section.
		/// </summary>
		protected string mName;
		/// <summary>
		/// Public constructor which takes a name for the Section.
		/// </summary>
		/// <param name="Name">Name of the Section</param>
		public Section (string Name)
		{
			mName = Name;
		}
		/// <summary>
		/// Public read only property for the name of the Section.
		/// </summary>
		public string Name
		{
			get
			{
				return mName;
			}
		}
		/// <summary>
		/// This function is called for all Sections for a Config object when its underlying file has been
		/// changed and FileWatchEnabled is set to true.
		/// If the Variables in the Items of this Section doesn't match the Variables in the file,
		/// the ConfigFileSectionChanged event is raised.
		/// </summary>
		public void CheckConfigFileSectionChange ()
		{
			bool SectionChanged = false;
			foreach (KeyValuePair<string, Item> kvp in this) {
				if (kvp.Value.CheckFileItemChange()) {
					SectionChanged = true;
				}
			}
			if (SectionChanged && (OnConfigFileSectionChanged != null)) {
				OnConfigFileSectionChanged(this, new CfgEventArgs());
			}
		}
	}
	/// <remarks>
	/// The SectionDict class which extends a Dictionary for Sections. The key is the name of the Section
	/// and the value is the Section object.
	/// </remarks>
	public class SectionDict : Dictionary<string, Section>
	{
		/// <summary>
		/// Create and add a Section object to the Section dictionary.
		/// The Section is only added if it doesn't already exist.
		/// The Section is identified by its name. All Sections in one Config file must have an unique name.
		/// </summary>
		/// <param name="SectionName">The name of the Section to be created</param>
		/// <returns>Returns the newly created Section object or the already existing Section with the specified name</returns>
		public Section AddSection (string SectionName)
		{
			Section s;
			if (!TryGetValue(SectionName, out s)) {
				s = new Section(SectionName);
				Add(SectionName, s);
			}
			return s;
		}
		/// <summary>
		/// Get the specified Variable in the specified Section from the Section dictionary
		/// with the specified Item from the Item dictionary in that Section.
		/// </summary>
		/// <param name="SectionName">Name of the Section where the variable exists.</param>
		/// <param name="ItemName">Name of the Item where the variable exists.</param>
		/// <param name="VariableName">The name of the Variable.</param>
		/// <returns>A CfgVarNode object for the Variable. Null if any of the Variable, Item or Section doesn't exist</returns>
		public CfgVarNode FindVariable (string SectionName, string ItemName, string VariableName)
		{
			Section s;
			if (TryGetValue(SectionName, out s))
				return s.FindVariable(ItemName, VariableName);
			else
				return null;
		}
		/// <summary>
		/// Saves a specified Section to the underlying file object.
		/// </summary>
		/// <param name="SectionName">Name of the Section</param>
		public void SaveToFileConf (string SectionName)
		{
			Section s;
			if (TryGetValue(SectionName, out s)) {
				s.SaveToFileConf();
			}
		}
		/// <summary>
		/// Saves all Sections in the dictionary to the underlying file object.
		/// </summary>
		public void SaveAll ()
		{
			foreach (KeyValuePair<string, Section> kvp in this) {
				kvp.Value.SaveToFileConf();
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Svep DesignCenter
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions