Custom Xml Application Configuration Library

[Back] [Main]


using System;
using System.Xml;
using System.Configuration;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using Accounting.BaseClasses;


namespace Accounting.GUI
{
	/// 
	/// Summary description for AppConfig.
	/// Configuration loads data automatically from config.cfg, placed
	/// in the same directory as application executable
	/// Config holds its data in objects field.
	/// You can add new config object using AddObject function,
	/// if first char of the key is '@' the object assumed being temporary
	/// and did not save to file
	/// You can also delete any object using its key (name) - function
	/// DelObject
	/// All changes saved automatically
	/// 
	public class AppConfig
	{
		public static System.Collections.Hashtable objects;

		
		static AppConfig()
		{
			objects = new System.Collections.Hashtable ();		
			load_config();					
		}

		private static void load_config()
		{				
			System.Xml.XmlTextReader xtr = new XmlTextReader (System.IO.Path.GetDirectoryName  (System.Windows.Forms.Application.ExecutablePath)+"\\config.cfg");
			try
			{
				xtr.ReadStartElement ();
				while (xtr.Name == "object")
				{
					string key  = xtr.GetAttribute ("key");
					string type = xtr.GetAttribute ("type");

					System.Type tp = System.Type.GetType (type);				
					System.Xml.Serialization.XmlSerializer xmlSrz = new System.Xml.Serialization.XmlSerializer(tp);			
					xtr.ReadStartElement ();
					object obj;
					obj = xmlSrz.Deserialize (xtr);					
					objects.Add (key,obj);

						if ( xtr.NodeType == System.Xml.XmlNodeType.EndElement ) xtr.ReadEndElement();
					else
						if (xtr.NodeType == System.Xml.XmlNodeType.Element) xtr.ReadStartElement ();
					
				}
				xtr.ReadEndElement ();
			}
			catch
			{
			}
			finally
			{	
			 xtr.Close ();		
			}
		}

		private static void save_config()
		{
			System.Xml.XmlTextWriter xtw = new XmlTextWriter (System.IO.Path.GetDirectoryName  (System.Windows.Forms.Application.ExecutablePath)+"\\config.cfg",System.Text.Encoding.Unicode);
			xtw.WriteStartDocument (true);
			xtw.WriteStartElement ("objects");
			foreach (object kk in objects.Keys)
			{
			 string kk_str = kk.ToString ();
				if (kk_str==null || kk_str[0]!='@' )
				{
					xtw.WriteStartElement ("object");
					xtw.WriteAttributeString ("key",kk.ToString ());
				    xtw.WriteAttributeString ("type",objects[kk].GetType().ToString ());					
 				    System.Xml.Serialization.XmlSerializer xmlSrz2 = new System.Xml.Serialization.XmlSerializer (objects[kk].GetType ());			
					xmlSrz2.Serialize (xtw,objects[kk]);		
					xtw.WriteEndElement ();
				}
			}
			xtw.WriteEndElement ();
			xtw.WriteEndDocument ();
			xtw.Flush ();
			xtw.Close ();			
		}

		public static void DelObject(string key_)
		{
			foreach (string kk in objects.Keys)
			{
				if (kk==key_)
				{
					objects.Remove (key_);
					if (key_[0]!='@')save_config();
					return;
				}
			}

		}

		public static void AddObject(string key_, object val_)
		{		
			foreach (string kk in objects.Keys)
			{
				if (kk==key_)
				{
					objects[key_] = val_;
					if (key_[0]!='@')save_config();
					return;
				}
			}

			objects.Add (key_,val_);	
			if (key_[0]!='@')save_config();
		}

	}

}
    

Last updated: 28 may 2007