首页  ·  知识 ·  编程语言
读取Web.config文件中的配置信息类
网友   http://www.cnblogs.com/snlfq2000/archive/2010/07/11/1775389.html  .NET  编辑:德仔   图片来源:网络
using System; using System.Web; using System.Collections.Generic; using System.Configura

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

    }


}

本文作者:网友 来源: http://www.cnblogs.com/snlfq2000/archive/2010/07/11/1775389.html
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读