using Microsoft.Extensions.Configuration; using System.IO; class Program { static void Main(string[] args) { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); IConfiguration config = builder.Build(); // access configuration values string connectionString = config.GetConnectionString("DefaultConnection"); string logLevel = config["Logging:LogLevel:Default"]; } }
using Microsoft.Extensions.Configuration; using System.IO; class Program { static void Main(string[] args) { string basePath = @"C:\myapp\configs"; var builder = new ConfigurationBuilder() .SetBasePath(basePath) .AddJsonFile("appsettings.json"); IConfiguration config = builder.Build(); // access configuration values string connectionString = config.GetConnectionString("DefaultConnection"); string logLevel = config["Logging:LogLevel:Default"]; } }This example sets the base path to an absolute path and adds an appsettings.json file to the configuration. The values in the configuration file can then be accessed using the IConfiguration interface. These examples demonstrate the use of SetBasePath method in ConfigurationBuilder class in the Microsoft.Extensions.Configuration package library to specify the root directory path of the configuration file.