Environment::GetEnvironmentVariable Method (String^)


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.

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

static String^ GetEnvironmentVariable(
	String^ variable


Type: System::String^

The name of the environment variable.

Return Value

Type: System::String^

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

Exception Condition

variable is null.


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

The GetEnvironmentVariable(String^) method retrieves an environment variable from the environment block of the current process only. It is equivalent to calling the GetEnvironmentVariable(String^, EnvironmentVariableTarget) method with a target value of EnvironmentVariableTarget::Process. The environment block of the current process includes the following environment variables:

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.

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

Environment variable names are not case-sensitive.

The following example uses the GetEnvironmentVariable method to retrieve the windir environment variable, which contains the path of the Windows directory.

using namespace System;
using namespace System::IO;

void main()
      // Change the directory to %WINDIR%
      Environment::CurrentDirectory = Environment::GetEnvironmentVariable( "windir" );
      DirectoryInfo^ info = gcnew DirectoryInfo( "." );

      Console::WriteLine("Directory Info:   {0}", info->FullName);
// The example displays output like the following:
//        Directory Info:   C:\windows

The following example attempts to retrieve the value of an environment variable named Test1 from the process environment block. If the variable doesn't exist, the example creates its and retrieves its value. The example displays the value of the variable. If the example created the variable, it also calls the GetEnvironmentVariables(EnvironmentVariableTarget) method with each member of the EnvironmentVariableTarget enumeration to establish that the variable can be retrieved only from the current process environment block. Finally, if the example created the variable, it deletes it.

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


for the ability to read the value of variable. Associated enumeration: EnvironmentPermissionAccess::Read

Universal Windows Platform
Available since 10
.NET Framework
Available since 1.1
Return to top