专注

导航

web.config操作类

   1:      /// <summary>
   2:      /// web.config操作类
   3:      /// Copyright (C) Maticsoft
   4:      /// </summary>
   5:      public sealed class ConfigHelper
   6:      {
   7:          /// <summary>
   8:          /// 得到AppSettings中的配置字符串信息
   9:          /// </summary>
  10:          /// <param name="key"></param>
  11:          /// <returns></returns>
  12:          public static string GetConfigString(string key)
  13:          {
  14:              string CacheKey = "AppSettings-" + key;
  15:              object objModel = DataCache.GetCache(CacheKey);
  16:              if (objModel == null)
  17:              {
  18:                  try
  19:                  {
  20:                      objModel = ConfigurationManager.AppSettings[key]; 
  21:                      if (objModel != null)
  22:                      {                        
  23:                          DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
  24:                      }
  25:                  }
  26:                  catch
  27:                  { }
  28:              }
  29:              return objModel.ToString();
  30:          }
  31:   
  32:          /// <summary>
  33:          /// 得到AppSettings中的配置Bool信息
  34:          /// </summary>
  35:          /// <param name="key"></param>
  36:          /// <returns></returns>
  37:          public static bool GetConfigBool(string key)
  38:          {
  39:              bool result = false;
  40:              string cfgVal = GetConfigString(key);
  41:              if(null != cfgVal && string.Empty != cfgVal)
  42:              {
  43:                  try
  44:                  {
  45:                      result = bool.Parse(cfgVal);
  46:                  }
  47:                  catch(FormatException)
  48:                  {
  49:                      // Ignore format exceptions.
  50:                  }
  51:              }
  52:              return result;
  53:          }
  54:          /// <summary>
  55:          /// 得到AppSettings中的配置Decimal信息
  56:          /// </summary>
  57:          /// <param name="key"></param>
  58:          /// <returns></returns>
  59:          public static decimal GetConfigDecimal(string key)
  60:          {
  61:              decimal result = 0;
  62:              string cfgVal = GetConfigString(key);
  63:              if(null != cfgVal && string.Empty != cfgVal)
  64:              {
  65:                  try
  66:                  {
  67:                      result = decimal.Parse(cfgVal);
  68:                  }
  69:                  catch(FormatException)
  70:                  {
  71:                      // Ignore format exceptions.
  72:                  }
  73:              }
  74:   
  75:              return result;
  76:          }
  77:          /// <summary>
  78:          /// 得到AppSettings中的配置int信息
  79:          /// </summary>
  80:          /// <param name="key"></param>
  81:          /// <returns></returns>
  82:          public static int GetConfigInt(string key)
  83:          {
  84:              int result = 0;
  85:              string cfgVal = GetConfigString(key);
  86:              if(null != cfgVal && string.Empty != cfgVal)
  87:              {
  88:                  try
  89:                  {
  90:                      result = int.Parse(cfgVal);
  91:                  }
  92:                  catch(FormatException)
  93:                  {
  94:                      // Ignore format exceptions.
  95:                  }
  96:              }
  97:   
  98:              return result;
  99:          }
 100:      }

posted on 2011-11-13 11:42  陈啊M  阅读(211)  评论(0编辑  收藏  举报