forked from laochiangx/Common.Utility
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigHelper.cs
More file actions
172 lines (154 loc) · 4.21 KB
/
ConfigHelper.cs
File metadata and controls
172 lines (154 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.Configuration;
namespace Common.Utility
{
/// <summary>
/// web.config操作类
/// </summary>
public sealed class ConfigHelper
{
/// <summary>
/// 得到AppSettings中的配置字符串信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetConfigString(string key)
{
string CacheKey = "AppSettings-" + key;
object objModel = DataCache.GetCache(CacheKey);
if (objModel == null)
{
try
{
objModel = ConfigurationManager.AppSettings[key];
if (objModel != null)
{
DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
}
}
catch
{ }
}
return objModel.ToString();
}
/// <summary>
/// 得到AppSettings中的配置Bool信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool GetConfigBool(string key)
{
bool result = false;
string cfgVal = GetConfigString(key);
if(null != cfgVal && string.Empty != cfgVal)
{
try
{
result = bool.Parse(cfgVal);
}
catch(FormatException)
{
// Ignore format exceptions.
}
}
return result;
}
/// <summary>
/// 得到AppSettings中的配置Decimal信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static decimal GetConfigDecimal(string key)
{
decimal result = 0;
string cfgVal = GetConfigString(key);
if(null != cfgVal && string.Empty != cfgVal)
{
try
{
result = decimal.Parse(cfgVal);
}
catch(FormatException)
{
// Ignore format exceptions.
}
}
return result;
}
/// <summary>
/// 得到AppSettings中的配置int信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static int GetConfigInt(string key)
{
int result = 0;
string cfgVal = GetConfigString(key);
if(null != cfgVal && string.Empty != cfgVal)
{
try
{
result = int.Parse(cfgVal);
}
catch(FormatException)
{
// Ignore format exceptions.
}
}
return result;
}
#region 添加 AppConfig
/// <summary>
/// 设置AppConfig
/// </summary>
/// <param name="key">键</param>
/// <param name="val">值</param>
public static void AddAppConfig(string key, string val)
{
ConfigurationManager.AppSettings.Add(key, val);
}
#endregion
#region 更新 AppConfig
/// <summary>
/// 设置AppConfig
/// </summary>
/// <param name="key">键</param>
/// <param name="val">值</param>
public static void SetAppConfig(string key,string val)
{
ConfigurationManager.AppSettings.Set(key, val);
string CacheKey = "AppSettings-" + key;
// DotNet.Common.DataCache.SetCache(CacheKey, val, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
}
#endregion
#region 移除 AppConfig
/// <summary>
/// Remove AppConfig
/// </summary>
/// <param name="key">键</param>
/// <param name="val">值</param>
public static void RemoveAppConfig(string key)
{
ConfigurationManager.AppSettings.Remove(key);
}
#endregion
#region 得到AppSettings中的配置字符串信息
/// <summary>
/// 得到AppSettings中的配置字符串信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
#endregion
#region 得到AppSettings中的配置字符串信息 没有缓存
/// <summary>
/// 得到AppSettings中的配置字符串信息 没有缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetNoCacheConfig(string key)
{
return ConfigurationManager.AppSettings[key].ToString();
}
#endregion
}
}