https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?tabs=basicconfiguration
Nuget 添加
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.Options.ConfigurationExtensions
using Microsoft.Extensions.Configuration; using System; using System.IO; namespace RConsoleCore { class Program { public static IConfiguration Configuration { get; set; } static void Main(string[] args) { // 读取配置文件 var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); // 方式1 直接读取节点 Console.WriteLine($"{Configuration.GetSection("Version").Value}"); // 方式2 绑定对象 MySettings app = new MySettings(); Configuration.GetSection("appsetting").Bind(app); // 方式3 绑定对象(推荐方式) MySettings app2 = Configuration.GetSection("appsetting").Get<MySettings>(); Console.ReadKey(); } } }
原文地址:https://www.cnblogs.com/james641/p/8760582.html
时间: 2024-10-25 02:54:57