Environment.GetEnvironmentVariable Method (String)


Retrieves the value of an environment variable from the current process.

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

public 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 System;
using System.IO;

public class Example
   public static void Main()
      // Change the directory to %WINDIR%
      Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir");
      DirectoryInfo info = new DirectoryInfo(".");

      Console.WriteLine("Directory Info:   " + 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.

using System;

public class Example
   public static void Main()
      string value;
      bool toDelete = false;

      // Check whether the environment variable exists.
      value = Environment.GetEnvironmentVariable("Test1");
      // If necessary, create it.
      if (value == null) {
         Environment.SetEnvironmentVariable("Test1", "Value1");
         toDelete = true;

         // Now retrieve it.
         value = Environment.GetEnvironmentVariable("Test1");
      // Display the value.
      Console.WriteLine("Test1: {0}\n", value);

      // Confirm that the value can only be retrieved from the process
      // environment block.
      Console.WriteLine("Attempting to retrieve Test1 from:");
      foreach (EnvironmentVariableTarget enumValue in 
                         Enum.GetValues(typeof(EnvironmentVariableTarget))) {
         value = Environment.GetEnvironmentVariable("Test1", enumValue);
         Console.WriteLine("   {0}: {1}", enumValue, 
                           value != null ? value : "not found");

      // If we've created it, now delete it.
      if (toDelete) { 
         Environment.SetEnvironmentVariable("Test1", null);
         // Confirm the deletion.
         if (Environment.GetEnvironmentVariable("Test1") == null)
            Console.WriteLine("Test1 has been deleted.");
// The example displays the following output:
//       Test1: Value1
//       Attempting to retrieve Test1 from:
//          Process: Value1
//          User: not found
//          Machine: not found
//       Test1 has been deleted.


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