ConfigHelper.cs

来源:互联网 发布:会员积分系统源码 编辑:程序博客网 时间:2024/05/22 15:23
namespace Helpers{    using System;    using System.Configuration;    using System.Linq;    using System.ServiceModel.Configuration;    public static class ConfigHelper    {        /// <summary>        /// 判断键是否存在        /// </summary>        /// <param name="key">键名</param>        /// <returns>        ///     true:   存在        ///     false:  不存在        /// </returns>        public static bool HasKey(string key) => ConfigurationManager.AppSettings.AllKeys.Contains(key);        /// <summary>        /// 设定键值        /// </summary>        /// <param name="key">键名</param>        /// <param name="value">键值</param>        /// <returns>        ///     true:设定成功        ///     false:设定失败        /// </returns>        public static bool SetKey(string key, object value)        {            try            {                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);                var hasKey = config.AppSettings.Settings.AllKeys.Contains(key);                if (hasKey)                {                    config.AppSettings.Settings[key].Value = value.ToString();                }                else                {                    config.AppSettings.Settings.Add(key, value.ToString());                }                config.Save(ConfigurationSaveMode.Modified);                ConfigurationManager.RefreshSection("appSettings");                return true;            }            catch (Exception)            {                return false;            }        }        /// <summary>        /// 获取键值        /// </summary>        /// <param name="key">键名</param>        /// <returns>键值</returns>        public static string GetKey(string key)        {            try            {                return ConfigurationManager.AppSettings[key];            }            catch (ConfigurationException)            {                return null;            }        }        /// <summary>        /// 获取键值        /// </summary>        /// <typeparam name="T">键值类型</typeparam>        /// <param name="parseFunc">转换方法</param>        /// <param name="key">键名</param>        /// <param name="defaultValue">默认键值</param>        /// <returns>键值</returns>        public static T GetKey<T>(Func<string, T> parseFunc, string key, T defaultValue = default(T))        {            try            {                var node = ConfigurationManager.AppSettings[key];                return !string.IsNullOrEmpty(node) ? parseFunc(node) : defaultValue;            }            catch (ConfigurationException)            {                return default(T);            }        }        /// <summary>        /// 获取根节点地址        /// </summary>        /// <param name="endpointName">根节点名称</param>        /// <returns>根节点地址</returns>        public static Uri GetEndpointAddress(string endpointName)        {            var clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;            return clientSection == null                ? null                : (from ChannelEndpointElement item in clientSection.Endpoints                   where item.Name == endpointName                   select item.Address).FirstOrDefault();        }        /// <summary>        /// 设定根节点地址        /// </summary>        /// <param name="endpointName">根节点名称</param>        /// <param name="address">根节点地址</param>        /// <returns>        ///     true:设定成功        ///     false:设定失败        /// </returns>        public static bool SetEndpointAddress(string endpointName, Uri address)        {            try            {                var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);                var clientSection = config.GetSection("system.serviceModel/client") as ClientSection;                if (clientSection == null) return false;                foreach (ChannelEndpointElement item in clientSection.Endpoints)                {                    if (item.Name != endpointName) continue;                    item.Address = address;                    break;                }                config.Save(ConfigurationSaveMode.Modified);                ConfigurationManager.RefreshSection("system.serviceModel");                return true;            }            catch (Exception)            {                return false;            }        }    }}

原创粉丝点击