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

原文地址:https://www.cnblogs.com/zhangqs008/p/3059774.html