Environment.GetEnvironmentVariable Method (String)

 

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

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

Public Shared Function GetEnvironmentVariable (
	variable As String
) As String

Parameters

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
ArgumentNullException

variable is null.

SecurityException

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.

Imports System.IO

Module Example
   Public Sub Main()
        ' Change the directory to %WINDIR%
        Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir")
        Dim info As New DirectoryInfo(".")
        Console.WriteLine("Directory Info:   " + info.FullName)
   End Sub
End Module
' 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.

Module Example
   Public Sub Main()
      Dim value As String 
      Dim toDelete As Boolean = False

      ' Check whether the environment variable exists.
      value = Environment.GetEnvironmentVariable("Test1")
      ' If necessary, create it.
      If value Is Nothing Then
         Environment.SetEnvironmentVariable("Test1", "Value1")
         toDelete = True

         ' Now retrieve it.
         value = Environment.GetEnvironmentVariable("Test1")
      End If
      ' Display the value.
      Console.WriteLine("Test1: {0}", value)
      Console.WriteLine()

      ' Confirm that the value can only be retrieved from the process
      ' environment block.
      Console.WriteLine("Attempting to retrieve Test1 from:")
      For Each enumValue As EnvironmentVariableTarget In 
                         [Enum].GetValues(GetType(EnvironmentVariableTarget))
         value = Environment.GetEnvironmentVariable("Test1", enumValue)
         Console.WriteLine("   {0}: {1}", enumValue, 
                           If(value IsNot Nothing, value, "not found"))
      Next
      Console.WriteLine()

      ' If we've created it, now delete it.
      If toDelete Then 
         Environment.SetEnvironmentVariable("Test1", Nothing)
         ' Confirm the deletion.
         If Environment.GetEnvironmentVariable("Test1") = Nothing Then
            Console.WriteLine("Test1 has been deleted.")
         End If
      End If         
   End Sub
End Module
' The example displays output like the following:
'       Test1: Value1
'       
'       Attempting to retrieve Test1 from:
'          Process: Value1
'          User: not found
'          Machine: not found
'       
'       Test1 has been deleted.

EnvironmentPermission

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
Show: