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 { /// /// web.config操作类 /// Copyright (C) Maticsoft /// public sealed class ConfigHelper { /// /// 得到AppSettings中的配置字符串信息 /// /// /// public static string GetConfigString(string key) { string CacheKey = "AppSettings-" + key; object objModel = CacheExtensions.GetCache(CacheKey); if (objModel == null) { try { objModel = ConfigurationManager.AppSettings[key]; if (objModel != null) { CacheExtensions.SetCache(CacheKey, objModel, Enums.CacheTimeType.ByHours, 3); } } catch { } } return objModel.ToString(); } /// /// 得到AppSettings中的配置Bool信息 /// /// /// 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; } /// /// 得到AppSettings中的配置Decimal信息 /// /// /// 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; } /// /// 得到AppSettings中的配置int信息 /// /// /// 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; } /// /// 修改AppSettings中的配置信息 /// /// /// 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(); } } /// /// 加载配置文件 app升级配置文件 /// /// public static Dictionary loadCfg() { string cfgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ApplicationBase) + Path.DirectorySeparatorChar + "appset" + Path.DirectorySeparatorChar + "appconfig.properties"; Dictionary cfg = new Dictionary(); 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; } } }