Environment.GetEnvironmentVariable Method (String, EnvironmentVariableTarget)

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Retrieves the value of an environment variable from the current process or from the Windows operating system registry key for the current user or local machine.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

static member GetEnvironmentVariable : 
        variable:string *
        target:EnvironmentVariableTarget -> string

Parameters

variable
Type: System.String

The name of an environment variable.

target
Type: System.EnvironmentVariableTarget

One of the EnvironmentVariableTarget values.

Return Value

Type: System.String

The value of the environment variable specified by the variable and target parameters, or null if the environment variable is not found.

Exception Condition
ArgumentNullException

variable is null.

ArgumentException

target is not a valid EnvironmentVariableTarget value.

SecurityException

The caller does not have the required permission to perform this operation.

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. However, environment variables added only to the environment block of the current process persist only for the duration of the process.

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

Environment variable names are not case-sensitive.

The following example creates environment variables for the Process, User, and Machine targets, checks whether the operating system registry contains the User and Machine environment variables, then deletes the environment variables.

No code example is currently available or this language may not be supported.

EnvironmentPermission

for the ability to read the value of variable if target is Process (Associated enumeration: EnvironmentPermissionAccess.Read),

or

for full access to environment variables if target is User or Machine (Associated enumeration: PermissionState.Unrestricted).

.NET Framework
Available since 2.0
Return to top
Show: