zoukankan      html  css  js  c++  java
  • 应用程序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(); }
                }
            }
        }


    }

  • 相关阅读:
    SVN操作异常
    VS2010安装MVC3
    (转)游戏类型
    (转)32位汇编指令 寄存器
    (转)#pragma 用法
    (转)UI库
    (转)简单实用的网游服务器架构
    (转)一个客户端网游市场分布的数据
    (转)源于魔兽!《植物大战僵尸》成功奥秘
    (转)【分析】中国网游行业上市公司投资分析之网易
  • 原文地址:https://www.cnblogs.com/jhabb/p/1881503.html
Copyright © 2011-2022 走看看