For some cases you might need to set environment variables for your .NET application.
This is true to .NET core too, when you are storing your application settings in these variables.
How to set up a script for the job
It’s handy to create a windows command line script for creating these variables, because when you are on a new work environment, than you don’t need to look up the keys and values.
So bring up your most-loved text editor, make your environment_variable_creator.bat, and add the following code:
@echo on SETX VARIABLE1 VALUE SETX PREFIX_VARIABLE2 true pause
If you are using .NET core’s configuration builder with a prefix like this:
ConfigurationBuilder cfgBuilder = new ConfigurationBuilder(); cfgBuilder.AddEnvironmentVariables("PREFIX_"); IConfigurationRoot configuration = cfgBuilder.Build(); var value = configuration["VARIABLE2"];
Then you need to set your variables with prefix “PREFIX_” in the bat file. In this scenario, VARIABLE1 can not be read, because it does not contain the prefix.
The code sets the variables only for your user account, not for others. You can parameterize the SETX command to set the settings for a different user, but i recommend, to run the script with a command line for the targeted user, because its much easier.
To run your script under other users name, then open your command prompt with ‘Run as… different user’