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; } } }}
阅读全文
0 0
- ConfigHelper.cs
- CodematicDemo Common部分的源码 ConfigHelper.cs
- ConfigHelper
- ConfigHelper 配置文件辅助类
- Raysoft.Framework.Config.ConfigHelper
- Java-ConfigHelper工具类
- org.hibernate.util.ConfigHelper.java
- CS..........................
- cs
- cs
- cs
- Cs
- cs
- cs
- cs
- CS
- cs
- cs,
- python基础之正则表达式和re模块
- JavaWeb Session过期验证
- 安卓开发中,小知识点:onConfigurationChanged方法未调用,onMeasure的使用
- 镜映自我与群体结构
- Capstone训练营5
- ConfigHelper.cs
- 【转】苹果开发者账号申请(2017最新版)
- 什么是数?什么是量?
- Twitch如何实现转码器比FFmepg性能提升65%?(上)
- 技术性能领先,阿里云网络产品全面升级为企业级
- 关于os模块中查看文件时间的几种用法
- 风控模型评估方法以及大数据风控模型概念
- nginx 反向代理注意事项
- linux下使用定时任务完成mysql定期备份