引用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. Restful 接口开发 完整版

    准备: springboot ssm框架 项目结构:注意 Swagger2Config要和springboot的启动类放在同一级 //pom所需的依赖 <dependency> <g ...

  2. Cocos Creator中使用事件中心

    export class EventCenter { /** 监听数组 */ private listeners = {}; /** * 注册事件 * @param name 事件名称 * @para ...

  3. win下开机不登陆系统自动运行程序的解决方案

    文章作者:姜南(Slyar) 文章来源:Slyar Home (www.slyar.com) 转载请注明,谢谢合作. Jet的电脑可以定时开机了,但是他希望XP系统启动后在不登陆用户的情况下运行锐捷和 ...

  4. 快速从一个空虚拟机,空idea打通提交spark

    https://www.cnblogs.com/xxbbtt/p/8143593.html #!/bin/bash # Install Spark on CentOS 7 yum install ja ...

  5. Solr的配置和在java中的使用

    Solr是一个全局站内搜索引擎,可以快速的搜索出结果. Solr依赖于tomcat,把Solr的war包放到tomcat中即可运行. 使用solr,需要在solr的schema.xml中配置solr与 ...

  6. CF1013B And

    There is an array with n elements a1, a2, ..., an and the number x. In one operation you can select ...

  7. 第六章 对象-javaScript权威指南第六版(三)

    6.3 删除内容 delete运算符可以删除对象的属性. delete运算符只能删除自有属性,不能删除继承属性. delete表达式删除成功或没有任何副作用时,它返回true. 6.4 检测属性 用i ...

  8. idhttp提交post带参数并带上cookie

    有这么一个提交连接 http://www.XXXXXX.com/test.php?p1=411328&p2=1&d1=HeroSkinList 一共有三个参数[p1]  [p2]  [ ...

  9. Vue 记录 Cannot read property '_withTask' of undefined

    第二次遇到,年前偶尔代码中频繁出现过,因为没影响到交互,赶工期中,没有去深究. 今天又遇到了, 在事件触发后,脚本报错,终止了界面交互. 最后查找到这里的原因,检查并移除无效业务事件,错误消失了. ( ...

  10. Hibernte

    什么是CRM?(了解) CRM(customer relationship management)即客户关系管理,是指企业用CRM技术来管理与客户之间的关系.在不同场合下,CRM可能是一个管理学术语, ...