.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

猜你喜欢

转载自www.cnblogs.com/tangge/p/10039298.html
今日推荐