Sometimes we need to get and set the value of operating system environment variables in .NET Core programs. This article demonstrates how to useEnvironment.GetEnvironmentVariablewithEnvironment.SetEnvironmentVariableMethod to get and set the value of operating system environment variables.
First, we use the Windows command line cmd to create an environment variable myEnvironmentValue in the Windows system,Note to run cmd (Run as administrator) as an administrator, The cmd command is as follows:
setx myEnvironmentValue "MyValue" /M
This command creates an environment variable myEnvironmentValue in the Windows system and sets its value to "MyValue". Note that the parameter /M specifies that we are setting system environment variables (not user environment variables).Details can be found here。
After the command is run, "SUCCESS" is displayed to indicate success:

Now when we look at the environment variables of the Windows system, we can find that the environment variable myEnvironmentValue we just created through the command line is as follows:

Next, we create a new .NET Core console project, the code is as follows, note that this code requires Visual Studio to run as administrator (Run as administrator):
using System; namespace NetCoreEnvironmentVariable { class Program { static void Main(string[] args) { string myEnvironmentValue = Environment.GetEnvironmentVariable("myEnvironmentValue", EnvironmentVariableTarget.Machine);//Get the value of the operating system environment variable myEnvironmentValue. If the environment variable myEnvironmentValue does not exist in the operating system, the Environment.GetEnvironmentVariable method will return null. Note that the parameter EnvironmentVariableTarget.Machine specifies that what we get is a system environment variable (not a user environment variable). Pay attention to Environment The .GetEnvironmentVariable method does not require our .NET Core program to be run as an administrator (Run as administrator) to successfully retrieve the value of the system environment variable Console.WriteLine($"myEnvironmentValue:" + myEnvironmentValue); myEnvironmentValue = "UpdatedValue";//The new value of the environment variable myEnvironmentValue Environment.SetEnvironmentVariable("myEnvironmentValue", myEnvironmentValue, EnvironmentVariableTarget.Machine);//Set the value of the operating system environment variable myEnvironmentValue. If the environment variable myEnvironmentValue does not exist in the operating system, then the Environment.SetEnvironmentVariable method will create an environment variable named myEnvironmentValue in the operating system. Note that the parameter EnvironmentVariableTarget.Machine specifies that we are setting the system environment Variables (not user environment variables), and this requires our .NET Core program to be run as administrator (Run as administrator), otherwise the Environment.SetEnvironmentVariable method will throw an exception myEnvironmentValue = Environment.GetEnvironmentVariable("myEnvironmentValue", EnvironmentVariableTarget.Machine);//Get the value of the operating system environment variable myEnvironmentValue again Console.WriteLine($"myEnvironmentValue:" + myEnvironmentValue); Console.WriteLine("Press key to end..."); Console.ReadKey(); } } }
Note that the Environment.GetEnvironmentVariable and Environment.SetEnvironmentVariable methodsEnvironmentVariableTargetThe parameter used to specify that we get and set isWindows operating systemThe system environment variable, EnvironmentVariableTarget is an enumerated type, and all its values are explained as follows:
| Field | ||
| Machine | 2 | Store or retrieve environment variables in the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment key of the Windows operating system registry.This value should only be used for .NET implementations running on Windows systems. |
| Process | 0 | Environment variables are stored in or retrieved from the environment block associated with the current process. |
| User | 1 | Store or retrieve environment variables in the HKEY_CURRENT_USER\Environment key of the Windows operating system registry.This value should only be used for .NET implementations running on Windows systems. |
You can see that the values Machine and User enumerated by EnvironmentVariableTarget are only valid for the Windows operating system, but not for the Linux operating system.
The result of running the above code is as follows:

Then we check the environment variables of the Windows system again and we can find that the value of the environment variable myEnvironmentValue has become "UpdatedValue":

After testing in ASP.NET Core, you can also use the Environment.GetEnvironmentVariable method to get the values of system environment variables and user environment variables in the Windows operating system.
About the EnvironmentVariableTarget.Process parameter
In the Linux operating system, the Environment.GetEnvironmentVariable and Environment.SetEnvironmentVariable methods are only valid for the EnvironmentVariableTarget.Process parameter. When the Environment.GetEnvironmentVariable method uses the EnvironmentVariableTarget.Process parameter to obtain environment variables, it actually reads the environment variables in the current .NET Core program process, and the environment variables in the .NET Core program process can come from the operating system. Environment variables, this process can be understood as:
- In the operating system, add a system environment variable named myEnvironmentValue
- The .NET Core program reads the environment variable myEnvironmentValue in the operating system into the .NET Core program process
- Use the Environment.GetEnvironmentVariable method of the EnvironmentVariableTarget.Process parameter to read the value of the environment variable myEnvironmentValue from the .NET Core program process
The second step of this process is actually delayed, so when you add an environment variable to the operating system, you find that the Environment.GetEnvironmentVariable method using the EnvironmentVariableTarget.Process parameter cannot read the environment variable, please Restart the computer and try to read again. Restarting the computer will cause the .NET Core program process to read the environment variables in the operating system, so when using the EnvironmentVariableTarget.Process parameter, pay attention to the problem of delay.