应用程序xml 配置文件抽象基类

using System;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;

namespace IpTerm
{

//版权所有: jhabb  邮箱:jhabb@163.com//   qq :75420724      欢迎讨论 转载请标明
    /// <summary>
    /// 应用程序参数配置抽象基类
    /// </summary>
    public abstract class ConfigurationBase
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ConfigurationBase()
        { }

        /// <summary>
        /// 反序列化
        /// </summary>
        public void Load(string fileName)
        {
            if (string.IsNullOrEmpty(fileName)) { return; }

            TextReader textReader = null;
            XmlSerializer serial = null;
            ConfigurationBase configuration = null;
            if (!File.Exists(fileName)) { Save(fileName); return; }
            try
            {
                textReader = new StreamReader(fileName);
                serial = new XmlSerializer(this.GetType());
                configuration = serial.Deserialize(textReader) as ConfigurationBase;
                textReader.Close();
                foreach (PropertyInfo propertyInfo in this.GetType().GetProperties())
                {
                    if (propertyInfo.CanWrite)
                    {
                        object value = this.GetType().GetProperty(propertyInfo.Name).GetValue(configuration, null);
                        propertyInfo.SetValue(this, value, null);
                    }
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.Message);
            }
            finally
            {
                if (textReader != null) { textReader.Close(); }
            }
        }

        /// <summary>
        ///  序列化
        /// </summary>
        public void Save(string fileName)
        {
            if (string.IsNullOrEmpty(fileName)) { return; }
            string directoryName = Path.GetDirectoryName(fileName);
            if (!string.IsNullOrEmpty(directoryName) && !Directory.Exists(directoryName))
            {
                DirectoryInfo dirInfo;
                try
                {
                    dirInfo = Directory.CreateDirectory(Path.GetDirectoryName(fileName));
                }
                catch (Exception e)
                {
                    Console.Out.WriteLine(e.Message);
                    return;
                }
            }

            TextWriter textWriter = null;
            XmlSerializer serial = null;
            try
            {
                textWriter = new StreamWriter(fileName);
                serial = new XmlSerializer(this.GetType());
                serial.Serialize(textWriter, this);
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.Message);
            }
            finally
            {
                if (textWriter != null) { textWriter.Close(); }
            }
        }
    }


}

posted @ 2010-11-19 09:53  落冰  阅读(232)  评论(0编辑  收藏  举报