zoukankan      html  css  js  c++  java
  • 读取Web.config文件中的配置信息类

    using System;
    using System.Web;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Linq;
    using System.Xml;

    namespace CommHelper
    {
        public class AppConfig
        {
            /// <summary>
            /// 获得配置属性的bool值
            /// </summary>
            /// <param name="key"></param>
            /// <returns>[True/False]</returns>
            public static bool GetBool(string key)
            {
                bool isBool = false;
                try
                {
                    isBool = Convert.ToBoolean(ConfigurationManager.AppSettings[key]);
                }
                catch {                
                }
                return isBool;
            }
      /// <summary>
      /// 获得配置属性的Int值
      /// </summary>
      /// <param name="key"></param>
      /// <returns></returns>
            public static int GetInt(string key)
            {
                int iValue = -1;
                try
                {
                    iValue = Convert.ToInt32(ConfigurationManager.AppSettings[key]);
                }
                catch { }
                return iValue;
            }     
      /// <summary>
      /// 获得配置属性的string值
      /// </summary>
      /// <param name="key"></param>
      /// <returns></returns>
            public static string GetString(string key)
            {
                return Convert.ToString(ConfigurationManager.AppSettings[key]);
            }
      /// <summary>
      /// 获得配置属性的Int值
      /// </summary>
      /// <param name="key"></param>
      /// <param name="name"></param>
      /// <returns></returns>
            public static int GetInt(string key, string name)
            {
                string str = GetString(key, name);
                if (str != null)
                {
                    return Convert.ToInt32(str);
                }
                return -99999;
            }
      /// <summary>
      /// 获得配置属性的string值
      /// </summary>
      /// <param name="key"></param>
      /// <param name="name"></param>
      /// <returns></returns>
            public static string GetString(string key, string name)
            {
                XmlDocument document = new XmlDocument();
                document.Load(HttpContext.Current.Server.MapPath("/web.config"));
                XmlElement element = (XmlElement)document.SelectSingleNode(string.Format("/configuration/extendSettings/add[@key='{0}']", key));
                if (element != null)
                {
                    return element.GetAttribute(name);
                }
                return null;
            }

            [Obsolete("This method is obsolete, it has been replaced by GetString method.", true)]
            public static string ReadAppSetting(string key)
            {
                return ConfigurationManager.AppSettings[key];
            }

        }


    }

  • 相关阅读:
    java中 Hex(十六进制)和byte[]相互转换
    byte[]和File相互转换
    Stringboot MultiPartFile 转 File
    mysql8.0+运行报错The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. 解决办法
    idea激活
    mysql8安装成功过后navicat连接失败
    AJAX的使用
    单文件上传和多文件上传实例
    监听器的使用
    SQL分页技术
  • 原文地址:https://www.cnblogs.com/snlfq2000/p/1775382.html
Copyright © 2011-2022 走看看