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

 
     {
{3

 /**//// <summary>
        /**//// <summary>4
 /// 得到AppSettings中的配置字符串信息
        /// 得到AppSettings中的配置字符串信息5
 /// </summary>
        /// </summary>6
 /// <param name="key"></param>
        /// <param name="key"></param>7
 /// <returns></returns>
        /// <returns></returns>8
 public static string GetConfigString(string key)
        public static string GetConfigString(string key)9

 
         {
{10
 string CacheKey = "AppSettings-" + key;
            string CacheKey = "AppSettings-" + key;11
 object objModel = LTP.Common.DataCache.GetCache(CacheKey);
            object objModel = LTP.Common.DataCache.GetCache(CacheKey);12
 if (objModel == null)
            if (objModel == null)13

 
             {
{14
 try
                try15

 
                 {
{16
 objModel = ConfigurationManager.AppSettings[key];
                    objModel = ConfigurationManager.AppSettings[key];17
 if (objModel != null)
                    if (objModel != null)18

 
                     {
{                        19
 LTP.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
                        LTP.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);20
 }
                    }21
 }
                }22
 catch
                catch23

 
                 { }
{ }24
 }
            }25
 return objModel.ToString();
            return objModel.ToString();26
 }
        }27

28

 /**//// <summary>
        /**//// <summary>29
 /// 得到AppSettings中的配置Bool信息
        /// 得到AppSettings中的配置Bool信息30
 /// </summary>
        /// </summary>31
 /// <param name="key"></param>
        /// <param name="key"></param>32
 /// <returns></returns>
        /// <returns></returns>33
 public static bool GetConfigBool(string key)
        public static bool GetConfigBool(string key)34

 
         {
{35
 bool result = false;
            bool result = false;36
 string cfgVal = GetConfigString(key);
            string cfgVal = GetConfigString(key);37
 if(null != cfgVal && string.Empty != cfgVal)
            if(null != cfgVal && string.Empty != cfgVal)38

 
             {
{39
 try
                try40

 
                 {
{41
 result = bool.Parse(cfgVal);
                    result = bool.Parse(cfgVal);42
 }
                }43
 catch(FormatException)
                catch(FormatException)44

 
                 {
{45
 // Ignore format exceptions.
                    // Ignore format exceptions.46
 }
                }47
 }
            }48
 return result;
            return result;49
 }
        }50

 /**//// <summary>
        /**//// <summary>51
 /// 得到AppSettings中的配置Decimal信息
        /// 得到AppSettings中的配置Decimal信息52
 /// </summary>
        /// </summary>53
 /// <param name="key"></param>
        /// <param name="key"></param>54
 /// <returns></returns>
        /// <returns></returns>55
 public static decimal GetConfigDecimal(string key)
        public static decimal GetConfigDecimal(string key)56

 
         {
{57
 decimal result = 0;
            decimal result = 0;58
 string cfgVal = GetConfigString(key);
            string cfgVal = GetConfigString(key);59
 if(null != cfgVal && string.Empty != cfgVal)
            if(null != cfgVal && string.Empty != cfgVal)60

 
             {
{61
 try
                try62

 
                 {
{63
 result = decimal.Parse(cfgVal);
                    result = decimal.Parse(cfgVal);64
 }
                }65
 catch(FormatException)
                catch(FormatException)66

 
                 {
{67
 // Ignore format exceptions.
                    // Ignore format exceptions.68
 }
                }69
 }
            }70

71
 return result;
            return result;72
 }
        }73

 /**//// <summary>
        /**//// <summary>74
 /// 得到AppSettings中的配置int信息
        /// 得到AppSettings中的配置int信息75
 /// </summary>
        /// </summary>76
 /// <param name="key"></param>
        /// <param name="key"></param>77
 /// <returns></returns>
        /// <returns></returns>78
 public static int GetConfigInt(string key)
        public static int GetConfigInt(string key)79

 
         {
{80
 int result = 0;
            int result = 0;81
 string cfgVal = GetConfigString(key);
            string cfgVal = GetConfigString(key);82
 if(null != cfgVal && string.Empty != cfgVal)
            if(null != cfgVal && string.Empty != cfgVal)83

 
             {
{84
 try
                try85

 
                 {
{86
 result = int.Parse(cfgVal);
                    result = int.Parse(cfgVal);87
 }
                }88
 catch(FormatException)
                catch(FormatException)89

 
                 {
{90
 // Ignore format exceptions.
                    // Ignore format exceptions.91
 }
                }92
 }
            }93

94
 return result;
            return result;95
 }
        }96
 }
    }97

来源:https://www.cnblogs.com/homezzm/archive/2009/11/27/1612065.html
标签