using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Configuration; namespace Mesnac.Basic { /// /// 应用程序配置文件访问辅助类 /// public class AppConfigHelper { /// /// 读取string配置项 /// /// AppSetting配置项的Key值 /// 默认值 /// 返回对应配置项的string类型value值 public static string GetAppSettingValue(string appSettingKey, string defaultValue) { string result = ConfigurationManager.AppSettings[appSettingKey]; if (String.IsNullOrEmpty(result)) { return defaultValue; } return result; } /// /// 读取bool配置项 /// /// AppSetting配置项的Key值 /// 默认值 /// 返回对应配置项的bool类型value值 public static bool GetAppSettingValue(string appSettingKey, bool defaultValue) { string result = ConfigurationManager.AppSettings[appSettingKey]; if (String.IsNullOrEmpty(result)) { return defaultValue; } bool boolResult = false; if (bool.TryParse(result, out boolResult)) { return boolResult; } else { return defaultValue; } } /// /// 读取int配置项 /// /// AppSetting配置项的Key值 /// 默认值 /// 返回对应配置项的int类型value值 public static int GetAppSettingValue(string appSettingKey, int defaultValue) { string result = ConfigurationManager.AppSettings[appSettingKey]; if (String.IsNullOrEmpty(result)) { return defaultValue; } int intResult = 0; if (int.TryParse(result, out intResult)) { return intResult; } else { return defaultValue; } } /// /// 读取double类型配置项 /// /// AppSetting配置项的Key值 /// 默认值 /// 返回对应配置项的double类型value值 public static double GetAppSettingValue(string appSettingKey, double defaultValue) { string result = ConfigurationManager.AppSettings[appSettingKey]; if (String.IsNullOrEmpty(result)) { return defaultValue; } double doubleResult = 0.0; if (double.TryParse(result, out doubleResult)) { return doubleResult; } else { return defaultValue; } } } }