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(); }
                }
            }
        }


    }

  • 相关阅读:
    2017-9-8-Linux下VNC server开启&图形界面显示
    2017-9-8-RaspberryPi安装过程
    2017-9-7-Linux Mint TFTP服务安装开启
    2017-9-7-第一篇博客
    面试回答优缺点问题
    多层板的层叠和压合结构
    磁珠和电感
    关于TVS、ESD、稳压二极管、压敏电阻
    STM8硬件设计注意事项
    根据电路板画出电路原理图的方法
  • 原文地址:https://www.cnblogs.com/jhabb/p/1881503.html
Copyright © 2011-2022 走看看