//*********************************************************************************
//Description:AppSettings配置文件帮助类
//Author:DennyHui
//Create Date: 2020年4月21日16:35:29
//*********************************************************************************
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.IO;
namespace Fisk.MDM.Utility.Common
{
public class AppsettingsHelper
{
public static readonly IConfiguration Configuration;
static AppsettingsHelper()
{
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true)
.Build();
}
public static T GetSection<T>(string key) where T : class, new()
{
try
{
var obj = new ServiceCollection()
.AddOptions()
.Configure<T>(Configuration.GetSection(key))
.BuildServiceProvider()
.GetService<IOptions<T>>()
.Value;
return obj;
}
catch (Exception)
{
return null;
}
}
public static string GetSection(string key)
{
try
{
return Configuration.GetValue<string>(key);
}
catch (Exception)
{
return string.Empty;
}
}
/// <summary>
/// 得到AppSettings中的配置字符串信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetConfigStr(string key)
{
object objModel = null;
try
{
objModel = Configuration.GetValue<string>(key);
return objModel.ToString();
}
catch
{
return string.Empty;
}
}
/// <summary>
/// 得到AppSettings中的配置Bool信息
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool GetConfigBool(string key)
{
bool result = false;
string cfgVal = GetConfigStr(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 = GetConfigStr(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 = GetConfigStr(key);
if (null != cfgVal && string.Empty != cfgVal)
{
try
{
result = int.Parse(cfgVal);
}
catch (FormatException)
{
// Ignore format exceptions.
}
}
return result;
}
}
}