C# 自定义配置文件辅助类(网站使用)

分类:C#     发布:2019-08-16     来源:本站     浏览:780 次
//--------------------------------------------------------------------------------
// 文件描述:配置文件辅助类
// 文件介绍:适用于网站参数配置,面向对象操作,对象类名将保存为网站根目录下/Config/[类名].config
//--------------------------------------------------------------------------------

using System;
using System.Configuration;
using System.IO;
using System.Security;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Configuration;
using System.Xml;
using System.Xml.Serialization;

namespace RC.Framework.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)
        {
            if (config == null)
            {
                return;
            }
            var configClassType = typeof(T);
            var 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 configClassType = typeof(T);
            var configObject = new object();
            var 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>()
        {
            var configDirPath = HttpContext.Current.Server.MapPath("~/config");
            if (!Directory.Exists(configDirPath))
            {
                Directory.CreateDirectory(configDirPath);
            }

            return Path.Combine(configDirPath, typeof(T).Name + ".config");
        }

        /// <summary>
        ///     写入配置文件
        /// </summary>
        /// <param name="config">配置文件内容</param>
        /// <param name="configPath">配置文件名称</param>
        public static void WriteConfig(string config, string configPath)
        {
            configPath = Path.Combine(Thread.GetDomain().BaseDirectory, configPath);
            if (File.Exists(configPath))
            {
                File.Delete(configPath);
            }
            using (var w = File.AppendText(configPath))
            {
                w.WriteLine(config);
                w.Flush();
            }
        }

        /// <summary>
        ///     读取配置文件
        /// </summary>
        /// <returns></returns>
        public static string ReadConfig<T>()
        {
            var configPath = GetConfigPath<T>();
            var configContent = string.Empty;
            if (File.Exists(configPath))
            {
                using (var sr = new StreamReader(configPath))
                {
                    configContent = sr.ReadToEnd();
                    sr.Close();
                }
            }
            return configContent;
        }

        //序列化单个对象:Serialize<UserInfo>(info) ;反序列化:Deserialize(typeof(UserInfo), s) as UserInfo
        //序列化集合对象:Serialize<List<UserInfo>>(list);反序列化:Deserialize(typeof(List<UserInfo>), s) as List<UserInfo>

        /// <summary>
        ///     序列化对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="t">对象</param>
        /// <returns></returns>
        public static string Serialize<T>(T t)
        {
            using (var sw = new StringWriter())
            {
                var xz = new XmlSerializer(t.GetType());
                xz.Serialize(sw, t);
                return sw.ToString();
            }
        }

        /// <summary>
        ///     反序列化为对象
        /// </summary>
        /// <param name="type">对象类型</param>
        /// <param name="s">对象序列化后的Xml字符串</param>
        /// <returns></returns>
        public static object Deserialize(Type type, string s)
        {
            using (var sr = new StringReader(s))
            {
                var xz = new XmlSerializer(type);
                return xz.Deserialize(sr);
            }
        }

        /// <summary>
        ///     读取AppConfig里的配置节
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAppSetting(string key)
        {
            return ConfigurationManager.AppSettings[key] ?? string.Empty;
        }

        /// <summary>
        ///     设置应用程序配置节点,如果已经存在此节点,则会修改该节点的值,否则添加此节点
        /// </summary>
        /// <param name="key">节点名称</param>
        /// <param name="value">节点值</param>
        public static void SetAppSetting(string key, string value)
        {
            var config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
            var appSetting = (AppSettingsSection)config.GetSection("appSettings");
            if (appSetting.Settings[key] == null)
            {
                appSetting.Settings.Add(key, value);
            }
            else
            {
                appSetting.Settings[key].Value = value;
            }
            config.Save();
        }
    }
}
        

如果觉得文章对您有帮助,您可以对我进行打赏 ¥1.03 元(金额随机^_^,每次刷新金额不同)。

请使用支付宝扫码支付

留言评论

*称  呼:
*联系方式: 方便与您取得联系,推荐使用邮箱。
*内  容:

已有评论

暂无数据

上一篇:C# Http请求封装(携带cookie版)(用于模拟登录)

下一篇:C# DES对称加密(需自行配置秘钥)