System.Environment.GetEnvironmentVariable methods

This article provides supplementary remarks to the reference documentation for this API.

The GetEnvironmentVariable method retrieves the value of an environment variable from the current process.

Environment variable names are case-sensitive on Unix-like systems but aren't case-sensitive on Windows.

Note

In-process environment modifications made by native libraries aren't seen by managed callers. Conversely, such modifications made by managed callers aren't seen by native libraries.

GetEnvironmentVariable(String) method

The GetEnvironmentVariable(String) method retrieves an environment variable from the environment block of the current process only. It's equivalent to calling the GetEnvironmentVariable(String, EnvironmentVariableTarget) method with a target value of EnvironmentVariableTarget.Process.

To retrieve all environment variables along with their values, call the GetEnvironmentVariables method.

On Windows systems

On Windows systems, the environment block of the current process includes:

  • All environment variables that are provided to it by the parent process that created it. For example, a .NET application launched from a console window inherits all of the console window's environment variables.

    If there is no parent process, per-machine and per-user environment variables are used instead. For example, a new console window has all per-machine and per-user environment variables defined at the time it was launched.

  • Any variables added to the process block while the process is running by calling either the SetEnvironmentVariable(String, String) method or the SetEnvironmentVariable(String, String, EnvironmentVariableTarget) method with a target value of EnvironmentVariableTarget.Process. These environment variables persist until the .NET application terminates.

If environment variables are created after the process has started, you can use this method to retrieve only those variables that were created by calling the SetEnvironmentVariable(String, String) method or the SetEnvironmentVariable(String, String, EnvironmentVariableTarget) method with a target value of .EnvironmentVariableTarget.Process.

On Unix-like systems

On Unix-like systems, the environment block of the current process includes the following environment variables:

.NET on Unix-like systems does not support per-machine or per-user environment variables.

GetEnvironmentVariable(String, EnvironmentVariableTarget) method

To retrieve all environment variables along with their values, call the GetEnvironmentVariables method.

On Windows systems

On Windows, the target parameter specifies whether the environment variable is retrieved from the current process or from the Windows operating system registry key for the current user or local machine. All per-user and per-machine environment variables are automatically copied into the environment block of the current process, as are any other environment variables that are available to the parent process that created the .NET process. However, environment variables added only to the environment block of the current process by calling either the SetEnvironmentVariable(String, String) method or the SetEnvironmentVariable(String, String, EnvironmentVariableTarget) method with a target value of EnvironmentVariableTarget.Process persist only for the duration of the process.

On Unix-like systems

On Unix-like systems, the GetEnvironmentVariable(String, EnvironmentVariableTarget) method supports a target value of EnvironmentVariableTarget.Process only. Calls with a target value of EnvironmentVariableTarget.Machine or EnvironmentVariableTarget.User are not supported and return null.

Per-process environment variables are: