引用nuget

Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Binder
Microsoft.Extensions.Configuration.FileExtensions
Microsoft.Extensions.Configuration.EnvironmentVariables
Microsoft.Extensions.Configuration.Json
using Allspark.Core.Common.Json;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO; namespace Allspark.Core.Common.Config
{
public static class Configs
{
private const string DefaultConfigName = "appsettings.json";
public class ConfigCache
{
internal static readonly IConfigurationRoot ConfigRoot = null;
static ConfigCache()
{
try
{
string pathToContentRoot = Directory.GetCurrentDirectory(); string configFilePath = Path.Combine(pathToContentRoot, DefaultConfigName); if (!File.Exists(configFilePath))
{
throw new FileNotFoundException($"{DefaultConfigName}配置文件不存在!");
}
//用于构建基于键/值的配置设置,以便在应用程序中使用
IConfigurationBuilder builder = new ConfigurationBuilder()
.SetBasePath(pathToContentRoot)//将基于文件的提供程序的FileProvider设置为PhysicalFileProvider基本路径
.AddJsonFile(DefaultConfigName, optional: false, reloadOnChange: true)//在构建器的路径中添加JSON配置提供程序
.AddEnvironmentVariables();//添加读取的Microsoft.Extensions.Configuration.IConfigurationProvider来自环境变量的配置值 ConfigRoot = builder.Build(); }
catch (Exception)
{ }
finally
{ }
} }
/// <summary>
/// 根据名称读取指定配置文件。若无法读取到数据,则使用defaultValue的值
/// </summary>
/// <typeparam name="T">节点的类型,可传对象</typeparam>
/// <param name="name">配置文件节点名</param>
/// <param name="defaultValue">默认值</param>
/// <returns></returns>
public static T Get<T>(string name, T defaultValue)
{
if (ConfigCache.ConfigRoot == null)
{
// throw new NullReferenceException("配置文件加载异常!");
return defaultValue;
}
IConfigurationSection section = ConfigCache.ConfigRoot.GetSection(name); if (section == null)
{
throw new KeyNotFoundException($"{name}节点不存在!");
}
var config = section.Get<T>();
if (config == null)
return defaultValue; return config;
}
/// <summary>
/// 根据名称读取指定配置文件
/// </summary>
/// <typeparam name="T">节点的类型,可传对象</typeparam>
/// <param name="name">配置文件节点名</param>
/// <returns></returns>
public static T Get<T>(string name)
{
return Get<T>(name, default);
} public static string Set<T>(string key, T value)
{
string StrValue = JsonHelper.ObjectToJson(value);
IEnumerable<IConfigurationProvider> configProviders = ConfigCache.ConfigRoot.Providers;
foreach (IConfigurationProvider item in configProviders)
{
if (item.TryGet(key, out string itemValue))
{
item.Set(key, StrValue);
return StrValue;
}
}
return "";
} }
}

.net core 灵活读取配置文件的更多相关文章

  1. NET Core开发-读取配置文件Configuration

    ASP.NET Core开发-读取配置文件Configuration   ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NE ...

  2. ASP.NET Core开发-读取配置文件Configuration

    ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...

  3. ASP.NET Core开发-读取配置文件Configuration appsettings.json

    https://www.cnblogs.com/linezero/p/Configuration.html ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配 ...

  4. .net core 学习 读取配置文件

    在空项目中是没有配置文件的,首先要新建一个,配置文件内容如下,下面来读取各个内容 { "ConnectionStrings": { "DefaultConnection& ...

  5. .net core自定义读取配置文件

    新建完成后项目目录下有个 appsettings.json { "Logging": { "LogLevel": { "Default": ...

  6. asp.net core mvc 读取配置文件appsettings.json

    上一篇我们将了读取自定义配置文件.这篇我们讲一下asp.net core mvc里读取自带的配置文件 appsettings.json 首先创建个asp.net core mvc项目,项目里有Prog ...

  7. .net core中读取配置文件

    1)先看丑陋的方法 读取 appsettings.json   然后在 Startup 的 ConfigureServices() 方法中进行注入: public IConfigurationRoot ...

  8. .NET平台开源项目速览(20)Newlife.Core中简单灵活的配置文件

    记得5年前开始拼命翻读X组件的源码,特别是XCode,但对Newlife.Core 的东西了解很少,最多只是会用用,而且用到的只是九牛一毛.里面好用的东西太多了. 最近一年时间,零零散散又学了很多,也 ...

  9. 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件

    目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...

随机推荐

  1. TPshop之短信注册配置(阿里云)

      短信注册准备: 1.阿里云账号实名认证(申请地址: https://www.aliyun.com/ , 注意不是阿里大于短信平台) 步骤: 注册登录阿里云——找到控制台 ​ 鼠标放在左上角,弹出菜 ...

  2. 3DMath

    线与面相交的计算 https://zh.wikipedia.org/wiki/%E7%BA%BF%E9%9D%A2%E4%BA%A4%E7%82%B9 什么是参数方程? 参数是参变数的简称.它是研究运 ...

  3. Selenium自动化测试之元素定位

    步骤: 1.通过前端工具,查看元素的属性 2.通过属性定位 iddriver.findElement(By.id("kw")) namedriver.findElement(By. ...

  4. Convolutional Neural Network in TensorFlow

    翻译自Build a Convolutional Neural Network using Estimators TensorFlow的layer模块提供了一个轻松构建神经网络的高端API,它提供了创 ...

  5. C++构造函数和析构函数的调用顺序

    1.构造函数的调用顺序 基类构造函数.对象成员构造函数.派生类本身的构造函数 2.析构函数的调用顺序 派生类本身的析构函数.对象成员析构函数.基类析构函数(与构造顺序正好相反) 3.特例 局部对象,在 ...

  6. 从 Python 快速启动 CGI 服务器

    很多人知道 Python 3 可以快速启动一个 HTTP 服务器: $ python3 -m http.server 8000 今天我查阅 http.server 模块发现它支持运行 CGI 脚本,只 ...

  7. 【HTTP】---HTTP状态码详解

    https://en.wikipedia.org/wiki/List_of_HTTP_status_codes 1.百科名片 HTTP状态码(HTTP Status Code)是用以表示网页服务器HT ...

  8. Centos7安装jdk-12的详细过程

    Centos7安装jdk-12的详细过程 2019-04-12   21:23:24 一.下载JDK-12版本 链接地址:官方地址 下载:jdk-12_liunx-x64_bin.tar.gz 二.检 ...

  9. Ubuntu 服务器设置软件多用户访问

    假设在用户A下安装了软件xx 路径写入$home/.bashrc 这时该软件只有该用户可以使用 若要其他用户也能使用,只需要将该.bashrc拷贝到其他user的$home目录就行了

  10. ftp无法上传问题

    1.背景 ftp服务端和客户端一直未做任何改动,无法上传属于突发状态,除此客户端外其他客户端上传正常 客户端(SunOS系统)可以正常连接ftp的xxx21端口,但是传输数据(文件)时无法正常传输 上 ...