163 lines
5.0 KiB
C#
163 lines
5.0 KiB
C#
using AUTS.Services.Extensions;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Configuration;
|
|
|
|
namespace AUTS.Services.Tool
|
|
{
|
|
/// <summary>
|
|
/// web.config操作类
|
|
/// Copyright (C) Maticsoft
|
|
/// </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 = CacheExtensions.GetCache<object>(CacheKey);
|
|
if (objModel == null)
|
|
{
|
|
try
|
|
{
|
|
objModel = ConfigurationManager.AppSettings[key];
|
|
if (objModel != null)
|
|
{
|
|
CacheExtensions.SetCache(CacheKey, objModel, Enums.CacheTimeType.ByHours, 3);
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改AppSettings中的配置信息
|
|
/// </summary>
|
|
/// <param name="key"></param>
|
|
/// <returns></returns>
|
|
public static void UpdatetConfig(string key, string value)
|
|
{
|
|
Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~");
|
|
AppSettingsSection objAppSettings = (AppSettingsSection)objConfig.GetSection("appSettings");
|
|
if (objAppSettings != null)
|
|
{
|
|
objAppSettings.Settings[key].Value = value;
|
|
objConfig.Save();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 加载配置文件 app升级配置文件
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public static Dictionary<String, String> loadCfg()
|
|
{
|
|
string cfgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ApplicationBase)
|
|
+ Path.DirectorySeparatorChar + "appset" + Path.DirectorySeparatorChar + "appconfig.properties";
|
|
Dictionary<String, String> cfg = new Dictionary<string, string>();
|
|
using (StreamReader sr = new StreamReader(cfgPath))
|
|
{
|
|
while (sr.Peek() >= 0)
|
|
{
|
|
string line = sr.ReadLine();
|
|
if (line.StartsWith("#"))
|
|
{
|
|
continue;
|
|
}
|
|
int startInd = line.IndexOf("=");
|
|
string key = line.Substring(0, startInd);
|
|
string val = line.Substring(startInd + 1, line.Length - (startInd + 1));
|
|
if (!cfg.ContainsKey(key) && !string.IsNullOrEmpty(val))
|
|
{
|
|
cfg.Add(key, val);
|
|
}
|
|
}
|
|
}
|
|
|
|
return cfg;
|
|
}
|
|
}
|
|
}
|