读取配置文件

1. 添加引用

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;

列子:

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ],
  "ReflectionConfig":{
    "className": "ClassLibraryTest.dll",
    "dllName": "ClassLibraryTest.Class1"
  } 
  //"ReflectionConfig":"qqqqq"
}

 

2.代码

var config = new ConfigurationBuilder().AddJsonFile("appsettings.json"); //项目中的json文件名
IConfiguration configuration=  config.Build(); 
string reflectionConfigString=  configuration["ReflectionConfig"]; //ReflectionConfig 节点名称 结果:qqqqq
 string dllName = config["ReflectionConfig:dllName"]; //结果ClassLibraryTest.dll

config["exclude:0"] //结果node_modules

 

posted @ 2022-03-23 22:20  乌柒柒  阅读(36)  评论(0编辑  收藏  举报