.Net Core 2控制台应用程序设置转换

Ama*_*n B 5 appsettings .net-core-2.0

我正在尝试将appsettings转换添加到.net core 2控制台应用程序,例如

  • appSettings.json
  • appSettings.Test.json
  • appSettings.Prod.json

我发现以下代码可用于asp.net核心:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: 
true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: 
true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}
Run Code Online (Sandbox Code Playgroud)

但是,我不知道如何获取,env.EnvironmentName因为IHostingEnvironment控制台应用程序中没有。

任何帮助将不胜感激

Ama*_*n B 2

找不到其他任何东西,所以现在使用预处理器指令

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if

public Startup()
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: 
true)
Run Code Online (Sandbox Code Playgroud)
#if RELEASE
Run Code Online (Sandbox Code Playgroud)
        .AddJsonFile($"appsettings.Release.json", optional: 
true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}
Run Code Online (Sandbox Code Playgroud)