看好多人不懂在.NET CORE中如何读取配置文件,我这里分了两篇,上一篇介绍了怎样通过appsettings.json配置读取文件信息。这一篇教大家自定义配置文件:

1.在项目下创建配置文件

{
"FileMap": {
"ImgPath": "D:\\myfile\\misc\\NPSPower\\TemplateCore\\TemplateCore\\wwwroot\\UpImg\\",
"ImgWeb": "http://127.0.0.1:1994/UpImg/",
"FilePath": "D:\\myfile\\misc\\NPSPower\\TemplateCore\\TemplateCore\\wwwroot\\UpFile\\",
"FileWeb": "http://127.0.0.1:1994/UpFile/",
"VideoPath": "D:\\myfile\\misc\\NPSPower\\TemplateCore\\TemplateCore\\wwwroot\\UpVideo\\",
"VideoWeb": "http://127.0.0.1:1994/UpVideo/",
"Web": "http://127.0.0.1:1994/"
}
}

2.引用类库Microsoft.Extensions.Configuration.Json并创建配置文件操作类ConfigHelper.cs

Install-Package Microsoft.Extensions.Configuration.Json -Version 3.0.0

using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text; namespace Common
{
public class ConfigHelper
{
private static IConfiguration _configuration; static ConfigHelper()
{
//在当前目录或者根目录中寻找文件
var fileName = "Config/ManagerConfig.json"; var directory = AppContext.BaseDirectory;
directory = directory.Replace("\\", "/"); var filePath = $"{directory}/{fileName}";
if (!File.Exists(filePath))
{
var length = directory.IndexOf("/bin");
filePath = $"{directory.Substring(0, length)}/{fileName}";
} var builder = new ConfigurationBuilder()
.AddJsonFile(filePath, false, true); _configuration = builder.Build();
} public static string GetSectionValue(string key)
{
return _configuration.GetSection(key).Value;
}
}
}

3.在项目中读取配置文件

string ImgPath = ConfigHelper.GetSectionValue("FileMap:ImgPath");
return ImgPath;

开源地址:https://github.com/jiyuwu/TemplateCore

测试浏览效果:http://127.0.0.1:1994/home/TestConfig

 帮助到你的话请点个推荐,谢谢。

干货:.net core实现读取自定义配置文件,有源代码哦的更多相关文章

  1. [.NET Core] 简单读取 json 配置文件

    简单读取 json 配置文件 背景 目前发现网上的 .NET Core 读取配置文件有点麻烦,自己想搞个简单点的. .NET Core 已经不使用之前的诸如 app.config 和 web.conf ...

  2. Springboot读取自定义配置文件的几种方法

    一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...

  3. 中阶d03.2 JDBC联合properties使用,通过读取本地配置文件为代码传递参数

    * 使用properties读取本地配置文件为代码传递参数 * url.用户名.密码.驱动地址等配置可以在配置文件中使用 main package zj_1_JDBC.properties; impo ...

  4. SpringBoot读取自定义配置文件

    自定义配置文件 my-config.properties bfxy.title=bfxy bfxy.name=hello spring boot! bfxy.attr=12345 配置文件类 appc ...

  5. springboot读取自定义配置文件节点

    今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...

  6. 【NET Core】.NET Core中读取json配置文件

    在.NET Framework框架下应用配置内容一般都是写在Web.config或者App.config文件中,读取这两个配置文件只需要引用System.Configuration程序集,分别用 Sy ...

  7. (C)spring boot读取自定义配置文件时乱码解决办法

    这是入门的第三天了,从简单的hello spring开始,已经慢慢接近web的样子.接下来当然是读取简单的对象属性了. 于是按照网上各位大神教的,简单写了个对象book,如上一篇(B),其他配置不需要 ...

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

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

  9. net core 简单读取json配置文件

    //添加 json 文件路径 var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()) ...

随机推荐

  1. java8 Stream使用总结

    [前言] java8新特性 java8 函数接口 java8 Optional使用总结 Java 8 时间日期使用 java8 lambda表达式 1.流的介绍 Java8 中的 Stream 是对集 ...

  2. Flask基础(12)-->请求上下文和应用上下文

    请求上下文和应用上下文 请求上下文:可以简单理解为客户端与服务器之间数据交互请求的容器 请求上下文对象有:request.Session request:封装了HTTP请求的内容,针对的是http的请 ...

  3. chown、chgrp、chmod

    1.权限对应的数值 一开始理解权限对应的数值总是要去用二进制去算例如r-x是多少rwx是多少,后来才知道r就 是4,w就是2,x就是1,不管权限怎么变,他们对应的数值就是对应位相加.. 权限对于文件来 ...

  4. navicat工具 pymysql模块

    目录 一 IDE工具介绍(Navicat) 二 pymysql模块 一 IDE工具介绍(Navicat) 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具,我们使用Navi ...

  5. Angular2+之使用FormGroup、FormBuilder和Validators对象控制表单(取值、赋值、校验和是否可编辑等)

    1.要使用Angular自带的表单控制需要先引入相关模块(.ts文件): import { FormGroup, //表单对象类 FormBuilder, //表单生成工具类 Validators} ...

  6. Docker Registry 管理镜像

    一.使用Docker Registry管理镜像 a.登陆docker hub  https://www.docker.com/products/docker-hub b.创建仓库 c.推送镜像   打 ...

  7. 【百度地图】如何去掉百度LOGO

    只需要一步就解决问题,在CSS里加入一段代码即可: .anchorBL{display:none;}

  8. 你也可以写聊天程序 - C# Socket学习1

    简述 我们做软件工作的虽然每天都离不开网络,可网络协议细节却不是每个人都会接触和深入了解.我今天就来和大家一起学习下Socket,并写一个简单的聊天程序. 一些基础类 首先我们每天打开浏览器访问网页信 ...

  9. UVA - 12099 The Bookcase

    No wonder the old bookcase caved under the massive piles of books Tom had stacked on it. He had bett ...

  10. VBS定时关闭软件

    用TXT写以下代码另存为VBS格式,代码如下:doset bag=getobject("winmgmts:\\.\root\cimv2")set pipe=bag.execquer ...