zoukankan      html  css  js  c++  java
  • CustomerConfigHelper

    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>

  • 相关阅读:
    TCP/IP 13学习笔记
    代码注入的三种方法
    一个简单的GTK的例子程序
    打印同样一个数据,竟然出现不同的结果,解决方法。
    解决multiple definition of的方法
    anjuta的安装、配置以及第一个hello程序
    rhythmbox中文乱码的解决方法
    Windows 和 Linux开发工具对比
    debian(包括ubuntu)命令行下的中文支持
    如何解决warning: no newline at end of file?
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/5415491.html
Copyright © 2011-2022 走看看