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

        }


    }

  • 相关阅读:
    Azure PowerShell (7) 使用CSV文件批量设置Virtual Machine Endpoint
    Windows Azure Cloud Service (39) 如何将现有Web应用迁移到Azure PaaS平台
    Azure China (7) 使用WebMetrix将Web Site发布至Azure China
    Microsoft Azure News(4) Azure新D系列虚拟机上线
    Windows Azure Cloud Service (38) 微软IaaS与PaaS比较
    Windows Azure Cloud Service (37) 浅谈Cloud Service
    Azure PowerShell (6) 设置单个Virtual Machine Endpoint
    Azure PowerShell (5) 使用Azure PowerShell创建简单的Azure虚拟机和Linux虚拟机
    功能代码(1)---通过Jquery来处理复选框
    案例1.用Ajax实现用户名的校验
  • 原文地址:https://www.cnblogs.com/snlfq2000/p/1775382.html
Copyright © 2011-2022 走看看