首页 > 其他分享 >.NET CORE读取自定义配置文件到Configuration中

.NET CORE读取自定义配置文件到Configuration中

时间:2022-12-02 15:15:15浏览次数:76  
标签:CORE 读取 配置文件 filePath line public string 自定义

微软只提供了以下可以读取配置的方式,其中文件配置只能读取INI、JSON和XML文件,如果我想对其他自定义文件进行读取,那么我们可以使用自定义配置提供程序进行配置

 

 

比如,我想对以下格式的文件进行读取,而且只想读取上半部分内容

auto eth0
iface eth0 inet static
address 10.50.50.111
netmask 255.255.255.0
gateway 10.50.50.1
dns-nameserver 8.8.8.8
dns-nameserver 114.114.114.114

auto eth1
iface eth1 inet static
address 192.168.3.110
netmask 255.255.255.0
gateway 192.168.3.1
dns-nameserver 8.8.8.8
dns-nameserver 114.114.114.114

 

1、新建CustomConfigurationProvider文件,继承自ConfigurationProvider

核心:对Data赋值

public class CustomConfigurationProvider : ConfigurationProvider
{
    private readonly string _filePath;
    private readonly int _line;

    public CustomConfigurationProvider(string filePath,int line)
    {
        _filePath = filePath;
        _line = line;
    }

    public override void Load()
    {
        if (!File.Exists(_filePath)) return;
        var line = 0;
        var dic = new Dictionary<string, string>();
        foreach (var str in File.ReadAllLines(_filePath))
        {
            if (line >=_line) break;
            var strArry = str.Split(" ");
            if (strArry.Length > 1)
                dic[strArry[0]] = strArry[1];
            line++;
        }

        Data = dic;
    }
}

 

2、新建CustomConfigurationSource文件,继承自IConfigurationSource

public class CustomConfigurationSource : IConfigurationSource
{
    private readonly string _filePath;
    private readonly int _line;
    public CustomConfigurationSource(string filePath,int line)
    {
        _filePath = filePath;
        _line = line;
    }

    public IConfigurationProvider Build(IConfigurationBuilder builder) => new CustomConfigurationProvider(_filePath, _line);
}

 

3、新建扩展文件ConfigurationExtensions

public static class ConfigurationExtensions
{
    /// <summary>
    /// 读取自定义格式配置文件
    /// </summary>
    /// <param name="builder"></param>
    /// <param name="filePath">文件目录</param>
    /// <param name="line">读取到第几行为止</param>
    /// <returns></returns>
    public static IConfigurationBuilder AddCustomConfiguration(
        this IConfigurationBuilder builder,string filePath,int line)
    {
        return builder.Add(new CustomConfigurationSource(filePath,line));
    }
}

 

4、在程序中读取

var networkConfig = new ConfigurationBuilder()
            .AddCustomConfiguration(Path.Combine("netowrk", "interfaces"),7)
            .Build()

 

 

好啦,是不是很简单?想读取其他类型的文件,只需在ConfigurationProvider处进行编写自己的方法即可。

参考文档:

微软官方文档-自定义配置提供程序

杨中科-开发自己的配置提供者

标签:CORE,读取,配置文件,filePath,line,public,string,自定义
From: https://www.cnblogs.com/arthaslcm/p/16944504.html

相关文章

  • 用NetCore + ReactJS 实现一个前后端分离的网站 (4) 用户登录与授权
    用NetCore+ReactJS实现一个前后端分离的网站(4)用户登录与授权1.前言这几天学了一些前端的知识,用AntDesignPro的脚手架搭建了一个前端项目->这里。登录界面是现......
  • 第一章:开始使用ASP.NET Core (三)
    1.3 ASPNETCore如何工作?到目前为止,您应该对ASPNETCore是什么有一个基础的了解。在本节中,您将看到使用ASPNETCore构建的应用程序是如何工作的,从用户请求URL到如何在浏览......
  • 进程异常core文件分析
    1. core文件相关配置1.1 查看core文件是否开启Corefilesize为0代表关闭core文件,unlimited代表开启core文件。开启core文件以后,代表进程异常以后,会生成core文件。使用......
  • cxf中自定义拦截器限制IP
    importjava.util.List;importjavax.servlet.http.HttpServletRequest;importorg.apache.cxf.interceptor.Fault;importorg.apache.cxf.message.Message;importorg.apac......
  • 第一章:开始使用ASP.NET Core (二)
    1.2何时选择ASP.NETCore 希望您现在已经大致了解了ASP.NETCore是什么以及它是如何设计的。但问题仍然存在:你应该使用它吗?微软建议所有新的.NETweb开发都应该使用ASP.......
  • org.apache.catalina.core.StandardWrapperValve.invoke 在路径为/xxx的上下文中,Servl
    举个例子,后端向jsp中传递了一个参数是一个对象当jsp进行引用${bean.xxx}时bean中没有xxx属性,那么就会报这个错看看是不是jsp更新了对应的类没有更新解决方式很简单,要么j......
  • Power App Portal列表自定义筛选器
     因为Portal的列表筛选,只有三种样式,文本框、单选和下拉框,如果要用到其他的筛选那就不能配置它了,所以我们可以使用它的FetchXml的筛选,来达到我们想要的效果,例如做一个日......
  • vue自定义指令实现拖拽元素改变元素的宽度
    //自定义指令directives:{drag:{inserted:function(el){constdragDom=el;dragDom.style.cursor="e-resize";dragD......
  • 使用Tinyfox部署Web API 读取配置文件问题
    特别注意:不管是你发布的网站还是WEBAPI服务库,涉及到读取配置文件的,必须将配置内容统一放到“TinyFox.exe.config”文件里面来,否则执行会失败!在TinyFox的官方文档里面......
  • 第一章:开始使用ASP.NET Core (一)
    选择用一个新的框架学习和开发是一项巨大的投资,因此尽早确定它是否适合你很重要。本章提供了一些有关ASP.NETCore的背景知识,如:它是什么,它是如何工作的,以及为什么您应该考......