[C#] C#自定读取配置文件类 →→→→→进入此内容的聊天室

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

回复 "C#自定读取配置文件类"

这儿你可以回复上面这条便签

captcha