Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmey-Jiang authored Feb 5, 2018
1 parent 1803fce commit 1f8e068
Showing 1 changed file with 165 additions and 6 deletions.
171 changes: 165 additions & 6 deletions Utility基础类大全/配置文件操作类/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Common.Utility
{
/// <summary>
/// web.config操作类
/// web.config操作类
/// </summary>
public sealed class ConfigHelper
{
/// <summary>
/// 得到AppSettings中的配置字符串信息
/// 得到AppSettings中的配置字符串信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
Expand All @@ -35,7 +35,7 @@ public static string GetConfigString(string key)
}

/// <summary>
/// 得到AppSettings中的配置Bool信息
/// 得到AppSettings中的配置Bool信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
Expand All @@ -57,7 +57,7 @@ public static bool GetConfigBool(string key)
return result;
}
/// <summary>
/// 得到AppSettings中的配置Decimal信息
/// 得到AppSettings中的配置Decimal信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
Expand All @@ -80,7 +80,7 @@ public static decimal GetConfigDecimal(string key)
return result;
}
/// <summary>
/// 得到AppSettings中的配置int信息
/// 得到AppSettings中的配置int信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
Expand All @@ -102,5 +102,164 @@ public static int GetConfigInt(string key)

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>
public static string GetConfigString(string key)
{
string CacheKey = "AppSettings-" + key;
object objModel = DotNet.Common.DataCache.GetCache(CacheKey);
if (objModel == null)
{
try
{
objModel = ConfigurationManager.AppSettings[key];
if (objModel != null)
{
DotNet.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(180), TimeSpan.Zero);
}
}
catch
{ }
}
return objModel.ToString();
}

#endregion

#region 得到AppSettings中的配置字符串信息 没有缓存
/// <summary>
/// 得到AppSettings中的配置字符串信息 没有缓存
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetNoCacheConfig(string key)
{
return ConfigurationManager.AppSettings[key].ToString();
}

#endregion

#region 得到AppSettings中的配置Bool信息
/// <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;
}

#endregion

#region 得到AppSettings中的配置Decimal信息
/// <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;
}

#endregion

#region 得到AppSettings中的配置int信息
/// <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;
}
#endregion
}

}

0 comments on commit 1f8e068

Please sign in to comment.