You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.3 KiB
C#

1 year ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace Mesnac.Basic
{
/// <summary>
/// 应用程序配置文件访问辅助类
/// </summary>
public class AppConfigHelper
{
/// <summary>
/// 读取string配置项
/// </summary>
/// <param name="appSettingKey">AppSetting配置项的Key值</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应配置项的string类型value值</returns>
public static string GetAppSettingValue(string appSettingKey, string defaultValue)
{
string result = ConfigurationManager.AppSettings[appSettingKey];
if (String.IsNullOrEmpty(result))
{
return defaultValue;
}
return result;
}
/// <summary>
/// 读取bool配置项
/// </summary>
/// <param name="appSettingKey">AppSetting配置项的Key值</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应配置项的bool类型value值</returns>
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;
}
}
/// <summary>
/// 读取int配置项
/// </summary>
/// <param name="appSettingKey">AppSetting配置项的Key值</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应配置项的int类型value值</returns>
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;
}
}
/// <summary>
/// 读取double类型配置项
/// </summary>
/// <param name="appSettingKey">AppSetting配置项的Key值</param>
/// <param name="defaultValue">默认值</param>
/// <returns>返回对应配置项的double类型value值</returns>
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;
}
}
}
}