.netcore读取配置文件

setting.json

{
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ]
}

引用Nuget:
Microsoft.Extensions.ConfigurationMicrosoft.Extensions.Configuration.Json

        static void Main(string[] args)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory()) //SetBasePath设置配置文件所在路径
                .AddJsonFile("setting.json");

            var configRoot = builder.Build();

            var compilerOptionsvalue =configRoot.GetSection("compilerOptions").GetSection("noImplicitAny").Value;
            Console.WriteLine(compilerOptionsvalue); 
            //False
            
            var excludevalue = configRoot.GetSection("exclude").GetChildren();
            foreach (var item in excludevalue)
            {
                Console.WriteLine(item.Value);
            }
            //"node_modules","wwwroot"

            Console.Read();
        }

参考:https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/index?view=aspnetcore-2.1

简单方式

json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "compilerOptions": {
    "noImplicitAny": false,
    "noEmitOnError": true,
    "removeComments": false,
    "sourceMap": true,
    "target": "es5"
  },
  "exclude": [
    "node_modules",
    "wwwroot"
  ],
  "wizerd": [
    { "Name": "tangsan","Age":30  },
    { "Name": "niuchacha","Age":22 }
  ]
}

读取方式:

            Console.WriteLine($"Default = {this.Configuration["Logging:LogLevel:Default"]}");
            Console.WriteLine($"AllowedHosts = {this.Configuration["AllowedHosts"]}");
            Console.WriteLine($"noImplicitAny = {this.Configuration["compilerOptions:noImplicitAny"]}");
            Console.WriteLine($"target = {this.Configuration["compilerOptions:target"]}");
            Console.WriteLine($"exclude[0] = {this.Configuration["exclude:0"]}");
            Console.WriteLine($"exclude[1] = {this.Configuration["exclude:1"]}");
            Console.WriteLine($"wizerd[0].Name = {this.Configuration["wizerd:0:Name"]}");
            Console.WriteLine($"wizerd[1].Name = {this.Configuration["wizerd:1:Name"]}");

196558-20181217165411943-890628996.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值