CustomerConfigHelper
2016-04-21 08:19
369 查看
public static class CustomerConfigHelper
{
public static object _lockObject = new object();
private static string GetCustomConfigValue(string key)
{
string configFilePath = "~/Config/CustomConfig.config";
string configElementsName = "Add";
string configAttributeKeyName = "Key";
string configAttributeValueName = "Value";
return GetValue(key,configFilePath, configElementsName, configAttributeKeyName,configAttributeValueName);
}
private static string GetValue(string key,string configFilePath)
{
string configElementsName = "Add";
string configAttributeKeyName = "Key";
string configAttributeValueName = "Value";
return GetValue(key,configFilePath, configElementsName, configAttributeKeyName,configAttributeValueName);
}
private static string GetValue(string key, string configFilePath, string configElementsName, string configAttributeKeyName,string configAttributeValueName)
{
string _value = string.Empty;
string _path = HttpContext.Current.Server.MapPath(configFilePath);
XElement _element = XElement.Load(_path);
var _config = from config in _element.Elements(configElementsName) select config;
Dictionary<string, string> _dic = _config.ToDictionary(k => k.Attribute(configAttributeKeyName).Value, k => k.Attribute(configAttributeValueName).Value);
_dic.TryGetValue(key, out _value);
return _value;
}
/// <summary>
/// 获取自定义文件配置节点值:整形
/// </summary>
/// <param name="key">配置节点KEY</param>
/// <returns></returns>
public static int GetCustomConfigInt(string key)
{
return TypeHelper.ToInt(GetCustomConfigValue(key));
}
/// <summary>
/// 获取自定义文件配置节点值:整形
/// </summary>
/// <param name="key"></param>
/// <param name="configFilePath"></param>
/// <returns></returns>
public static int GetCustomConfigInt(string key, string configFilePath)
{
return TypeHelper.ToInt(GetValue(key, configFilePath));
}
/// <summary>
/// 获取自定义文件配置节点值:长整形
/// </summary>
/// <param name="key">配置节点KEY</param>
/// <returns></returns>
public static long GetCustomConfigLong(string key)
{
return TypeHelper.ToInt64(GetCustomConfigValue(key));
}
/// <summary>
/// 获取自定义文件配置节点值:长整形
/// </summary>
/// <param name="key"></param>
/// <param name="configFilePath"></param>
/// <returns></returns>
public static long GetCustomConfigLong(string key, string configFilePath)
{
return TypeHelper.ToInt64(GetValue(key, configFilePath));
}
/// <summary>
/// 获取自定义文件配置节点值:字符串
/// </summary>
/// <param name="key">配置节点KEY</param>
/// <returns></returns>
public static string GetCustomConfigString(string key)
{
return GetCustomConfigValue(key);
}
/// <summary>
/// 获取自定义文件配置节点值:字符串
/// </summary>
/// <param name="key"></param>
/// <param name="configFilePath"></param>
/// <returns></returns>
public static string GetCustomConfigString(string key, string configFilePath)
{
return GetValue(key, configFilePath);
}
}
<?xml version="1.0" encoding="utf-8" ?>
<customconfig>
<!--分页页码大小-->
<Add Key="PageSize" Value="10"></Add>
<!--图片路径-->
<Add Key="ImagesWebPath" Value="http://xxx.xx.xxx.xxx:9000"></Add>
</customconfig>
{
public static object _lockObject = new object();
private static string GetCustomConfigValue(string key)
{
string configFilePath = "~/Config/CustomConfig.config";
string configElementsName = "Add";
string configAttributeKeyName = "Key";
string configAttributeValueName = "Value";
return GetValue(key,configFilePath, configElementsName, configAttributeKeyName,configAttributeValueName);
}
private static string GetValue(string key,string configFilePath)
{
string configElementsName = "Add";
string configAttributeKeyName = "Key";
string configAttributeValueName = "Value";
return GetValue(key,configFilePath, configElementsName, configAttributeKeyName,configAttributeValueName);
}
private static string GetValue(string key, string configFilePath, string configElementsName, string configAttributeKeyName,string configAttributeValueName)
{
string _value = string.Empty;
string _path = HttpContext.Current.Server.MapPath(configFilePath);
XElement _element = XElement.Load(_path);
var _config = from config in _element.Elements(configElementsName) select config;
Dictionary<string, string> _dic = _config.ToDictionary(k => k.Attribute(configAttributeKeyName).Value, k => k.Attribute(configAttributeValueName).Value);
_dic.TryGetValue(key, out _value);
return _value;
}
/// <summary>
/// 获取自定义文件配置节点值:整形
/// </summary>
/// <param name="key">配置节点KEY</param>
/// <returns></returns>
public static int GetCustomConfigInt(string key)
{
return TypeHelper.ToInt(GetCustomConfigValue(key));
}
/// <summary>
/// 获取自定义文件配置节点值:整形
/// </summary>
/// <param name="key"></param>
/// <param name="configFilePath"></param>
/// <returns></returns>
public static int GetCustomConfigInt(string key, string configFilePath)
{
return TypeHelper.ToInt(GetValue(key, configFilePath));
}
/// <summary>
/// 获取自定义文件配置节点值:长整形
/// </summary>
/// <param name="key">配置节点KEY</param>
/// <returns></returns>
public static long GetCustomConfigLong(string key)
{
return TypeHelper.ToInt64(GetCustomConfigValue(key));
}
/// <summary>
/// 获取自定义文件配置节点值:长整形
/// </summary>
/// <param name="key"></param>
/// <param name="configFilePath"></param>
/// <returns></returns>
public static long GetCustomConfigLong(string key, string configFilePath)
{
return TypeHelper.ToInt64(GetValue(key, configFilePath));
}
/// <summary>
/// 获取自定义文件配置节点值:字符串
/// </summary>
/// <param name="key">配置节点KEY</param>
/// <returns></returns>
public static string GetCustomConfigString(string key)
{
return GetCustomConfigValue(key);
}
/// <summary>
/// 获取自定义文件配置节点值:字符串
/// </summary>
/// <param name="key"></param>
/// <param name="configFilePath"></param>
/// <returns></returns>
public static string GetCustomConfigString(string key, string configFilePath)
{
return GetValue(key, configFilePath);
}
}
<?xml version="1.0" encoding="utf-8" ?>
<customconfig>
<!--分页页码大小-->
<Add Key="PageSize" Value="10"></Add>
<!--图片路径-->
<Add Key="ImagesWebPath" Value="http://xxx.xx.xxx.xxx:9000"></Add>
</customconfig>
相关文章推荐
- 第七周项目1—两点间的距离—成员函数、友元函数和一般函数
- MyBatis中SQL映射的XML文件
- 第五周项目一—三角形类定义成员函数
- LeetCode 172. Factorial Trailing Zeroes
- 三角形5
- 响应式布局图片处理
- LeetCode 311. Sparse Matrix Multiplication(稀疏矩阵相乘)
- 详解css3弹性盒模型(Flexbox)
- Outlook使用技巧
- 数码相框笔记
- 华为在线测试--学英语
- android音视频点/直播模块开发
- 几种常见的编程命名方式
- android学习13——android egl hello world
- c#中获取cpu序列号、网卡mac地址、ip地址、硬盘序列号、登录用户名、pc类型、计算机名称、物理内存
- Linux用户配置sudo权限(visudo)
- 8天掌握EF的Code First开发系列之动手写第一个Code First应用
- python 深浅拷贝
- Java IO:同步、非堵塞式IO(NIO)
- Mini-Shell with pipeline