干货:.net core实现读取自定义配置文件,有源代码哦
看好多人不懂在.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实现读取自定义配置文件,有源代码哦的更多相关文章
- [.NET Core] 简单读取 json 配置文件
简单读取 json 配置文件 背景 目前发现网上的 .NET Core 读取配置文件有点麻烦,自己想搞个简单点的. .NET Core 已经不使用之前的诸如 app.config 和 web.conf ...
- Springboot读取自定义配置文件的几种方法
一.读取核心配置文件 核心配置文件是指在resources根目录下的application.properties或application.yml配置文件,读取这两个配置文件的方法有两种,都比较简单. ...
- 中阶d03.2 JDBC联合properties使用,通过读取本地配置文件为代码传递参数
* 使用properties读取本地配置文件为代码传递参数 * url.用户名.密码.驱动地址等配置可以在配置文件中使用 main package zj_1_JDBC.properties; impo ...
- SpringBoot读取自定义配置文件
自定义配置文件 my-config.properties bfxy.title=bfxy bfxy.name=hello spring boot! bfxy.attr=12345 配置文件类 appc ...
- springboot读取自定义配置文件节点
今天和大家分享的是自定义配置信息的读取:近期有写博客这样的计划,分别交叉来写springboot方面和springcloud方面的文章,因为springboot预计的篇章很多,这样cloud的文章就需 ...
- 【NET Core】.NET Core中读取json配置文件
在.NET Framework框架下应用配置内容一般都是写在Web.config或者App.config文件中,读取这两个配置文件只需要引用System.Configuration程序集,分别用 Sy ...
- (C)spring boot读取自定义配置文件时乱码解决办法
这是入门的第三天了,从简单的hello spring开始,已经慢慢接近web的样子.接下来当然是读取简单的对象属性了. 于是按照网上各位大神教的,简单写了个对象book,如上一篇(B),其他配置不需要 ...
- 【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...
- net core 简单读取json配置文件
//添加 json 文件路径 var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()) ...
随机推荐
- Redis-->Ubutn上的安装教程
Redis 安装 当前redis最新稳定版本是4.0.9 以下步骤可以跳过 最新稳定版本下载链接:http://download.redis.io/releases/redis-4.0.9.tar.g ...
- 【SQL server基础】手动创建数据库和表格
use master go if exists(select * from sysdatabases where name='learning') drop database learning go ...
- 简单动态字符串(SDS)
SDS 前提:在redis中,C字符串只会作为字符串字面量用在一些无须对字符串进行修改的地方,比如打印日志: redisLog(REDIS_WARNING, “Redis is ready to ex ...
- Web前端开发的应用和前景——web 1.0到web 3.0
Web前端开发的应用和前景--web 1.0到web 3.0 Web1.0:(只读时代) 以静态.单向阅读为主,网站内信息可以直接和其他网站信息进行交互,能通过第三方信息平台同时对多家网站信息进行整合 ...
- 如何安装PHPstorm并配置php运行环境运行php项
php是程序开发者常用的编程语言,那么它就需要一款简单实用的集成开发环境,也就是一款舒服的编辑器,今天笔者为大家介绍phpstorm这款软件,并详细的讲解如何用它配置php项目 工具/原料 笔 ...
- MongoDB 学习笔记之 Nested doc/DBRef (Spark)
Nested doc/DBRef (Spark) 实现: 运行结果:
- 基于.NET环境,C#语言 实现 TCP NAT
基于.NET平台和C#语言实现TCP NAT穿越 1.<C# WinForm 跨线程访问控件(实用简洁写法)> 2.<基于.NET环境,C#语言 实现 TC ...
- 《完美解决系列》Android5.0以上 Implicit intents with startService are not safe
在Android6.0上,使用了以下代码: Intent intent = new Intent(); intent.setAction("xxx.server"); bindSe ...
- A-02 梯度下降法
目录 梯度下降法 一.梯度下降法详解 1.1 梯度 1.2 梯度下降法和梯度上升法 1.3 梯度下降 1.4 相关概念 1.4.1 步长 1.4.2 假设函数 1.4.3 目标函数 二.梯度下降法流程 ...
- python编程基础之十
分支条件: 单一分支条件:if 条件 : 条件真运行... 双分支条件:if 条件 : 条件真运行else: 条件假运行... 多分支条件:if 条件1 : 条件1真运行elif 条件2 : 条件1假 ...