zoukankan      html  css  js  c++  java
  • ConfigHelper 配置文件辅助类

    using System;
    using System.Globalization;
    using System.IO;
    using System.Security;
    using System.Security.Cryptography;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace Helper
    {
        /// <summary>
        ///     配置文件辅助类
        /// </summary>
        public class ConfigHelper
        {
            /// <summary>
            ///     更新配置信息,将配置信息对象序列化至相应的配置文件中,文件格式为带签名的UTF-8
            /// </summary>
            /// <typeparam name="T">配置信息类</typeparam>
            /// <param name="config">配置信息</param>
            public static void UpdateConfig<T>(T config)
            {
                Type configClassType = typeof (T);
                string configFilePath = GetConfigPath<T>(); //根据配置文件名读取配置文件  
                try
                {
                    var xmlSerializer = new XmlSerializer(configClassType);
                    using (var xmlTextWriter = new XmlTextWriter(configFilePath, Encoding.UTF8))
                    {
                        xmlTextWriter.Formatting = Formatting.Indented;
                        var xmlNamespace = new XmlSerializerNamespaces();
                        xmlNamespace.Add(string.Empty, string.Empty);
                        xmlSerializer.Serialize(xmlTextWriter, config, xmlNamespace);
                    }
                }
                catch (SecurityException ex)
                {
                    throw new SecurityException(ex.Message, ex.DenySetInstance, ex.PermitOnlySetInstance, ex.Method,
                                                ex.Demanded, ex.FirstPermissionThatFailed);
                }
            }
    
    
            /// <summary>
            ///     获取配置信息
            /// </summary>
            /// <typeparam name="T">配置信息类</typeparam>
            /// <returns>配置信息</returns>
            public static T GetConfig<T>() where T : class, new()
            {
                var configObject = new object();
                Type configClassType = typeof (T);
                string configFilePath = GetConfigPath<T>(); //根据配置文件名读取配置文件  
                if (File.Exists(configFilePath))
                {
                    using (var xmlTextReader = new XmlTextReader(configFilePath))
                    {
                        var xmlSerializer = new XmlSerializer(configClassType);
                        configObject = xmlSerializer.Deserialize(xmlTextReader);
                    }
                }
                var config = configObject as T;
                if (config == null)
                {
                    return new T();
                }
                return config;
            }
    
            /// <summary>
            ///     获取配置文件的服务器物理文件路径
            /// </summary>
            /// <typeparam name="T">配置信息类</typeparam>
            /// <returns>配置文件路径</returns>
            public static string GetConfigPath<T>()
            {
                string path = AppDomain.CurrentDomain.BaseDirectory;
                if (path == AppDomain.CurrentDomain.BaseDirectory)
                {
                    path = path + typeof (T).Name + ".config";
                }
                return path;
            }
    
    
            public static string GetDirPath(string dirName)
            {
                string dirPath = dirName;
                if (!Directory.Exists(dirPath))
                {
                    Directory.CreateDirectory(dirPath);
                }
                return dirPath;
            }
    
            public static string Md5(string input)
            {
                using (var md5 = new MD5CryptoServiceProvider())
                {
                    byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
                    return BitConverter.ToString(data).Replace("-", string.Empty).ToLower(CultureInfo.CurrentCulture);
                }
            }
    
            /// <summary>
            ///     读取配置文件
            /// </summary>
            /// <returns></returns>
            public static string ReadConfig<T>()
            {
                string configContent = string.Empty;
                string filePath = GetConfigPath<T>();
                if (File.Exists(filePath))
                {
                    using (var sr = new StreamReader(filePath, Encoding.Default))
                    {
                        configContent = sr.ReadToEnd();
                        sr.Close();
                    }
                }
                return configContent;
            }
    
            /// <summary>
            ///     写入配置文件
            /// </summary>
            /// <param name="config"></param>
            /// <returns></returns>
            public static void WriteConfig<T>(string config)
            {
                string fileName = GetConfigPath<T>();
                using (StreamWriter w = File.AppendText(fileName))
                {
                    w.WriteLine(config);
                    w.Close();
                }
            }
        }
    }

  • 相关阅读:
    django模型中的抽象类(abstract)
    http,tcp,udp的报文格式
    关于HTTP请求GET和POST的区别
    SQL语言分为四类,每类分别是?各包括什么?
    Python中为什么可以通过bin(n & 0xffffffff)来获得负数的补码?
    python中sorted和sorted 、reversed和reverse的使用。
    Django Cannot assign "A1": "B1" must be a "C1" instance. 错误信息
    python反转链表和成对反转
    Python单例模式的四种方法
    python的列表list和集合set操作
  • 原文地址:https://www.cnblogs.com/zhangqs008/p/3059774.html
Copyright © 2011-2022 走看看