title: How to set hosting environment name for .NET Core console app using Generic Host (HostBuilder)
body: <p>I am setting up a .NET Core 2.x console app as a Windows service and need to load an appsettings json file based on the environment.  I was thinking to start the service with a command line argument that identifies the environment.  How can I do that in Visual Studio?  No matter what I set in the project settings, the EnvironmentName value is always &quot;Production&quot;.</p>
  <p>How can I set my environment from a command line argument?</p>
  <pre><code>var hostBuilder = new HostBuilder()
                   .UseContentRoot(Directory.GetCurrentDirectory())
                   .ConfigureAppConfiguration((hostingContext, config) =&gt;
                   {
                       var env = hostingContext.HostingEnvironment;
                       config.AddCommandLine(args);
                       config.AddJsonFile($&quot;appsettings.{env.EnvironmentName}.json&quot;, optional: false, reloadOnChange: true);
                       config.AddEnvironmentVariables();
                   })
                   .ConfigureServices((hostContext, services) =&gt;
                   {
                       //Inject additional services as needed
                       services.AddHostedService&lt;JobRunner&gt;();
                  });
  </code></pre>
  <p><a href="https://i.stack.imgur.com/OOnEp.png" rel="noreferrer"><img src="https://i.stack.imgur.com/OOnEp.png" alt="enter image description here" /></a></p>