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

  • 相关阅读:
    [daily][archlinux][shell][fish] 使用最炫酷的shell工具fish
    [daily][btrfs][mlocate][updatedb] mlocate不认识btrfs里面的文件
    [daily][archlinux] TODO LIST
    [math][mathematica] mathematica入门
    [math][mathematica] archlinux 下 mathematica 的安装 (科学计算软件 mathematica/matlab/sagemath)
    [daily][centos][sudo] sudo 报错
    [development][C][thread_local] 线程全局变量
    [development][C] C语言标准
    [daily][centos][nginx] 在centos7使用nginx启用对文件目录的http访问
    CS RANK: AI & ML
  • 原文地址:https://www.cnblogs.com/zhangqs008/p/3059774.html
Copyright © 2020-2023  润新知