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];
            }

        }


    }

  • 相关阅读:
    ELM学习
    《进化》从孤胆极客到高效团队
    《人件》《PeopleWare》 【美】Tom DeMarco TimothyLister 著 肖然 张逸 滕云 译
    《进化》从孤胆极客到高效团队---Notes1
    大数据第一部分LInux学习Note1
    C#Windows窗体初学
    C#初学笔记(Windows编程的基本概念)
    C#学习2017-9-26(读取文本文件和读取二进制文件)Notes9
    C#学习2017-9-26Notes8(文件和流,FileStream类)
    C#学习笔记Notes8(接口,接口实现,程序集,命名空间,using)
  • 原文地址:https://www.cnblogs.com/snlfq2000/p/1775382.html
Copyright © 2011-2022 走看看