Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Interaction.Environ Method (String)

Returns the string associated with an operating-system environment variable.

Namespace:  Microsoft.VisualBasic
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
public static string Environ(
	string Expression
)

Parameters

Expression
Type: System.String

Required. Expression that evaluates either a string containing the name of an environment variable, or an integer corresponding to the numeric order of an environment string in the environment-string table.

Return Value

Type: System.String
Returns the string associated with an operating-system environment variable.
ExceptionCondition
ArgumentException

Expression is missing.

If Expression contains a string, the Environ function returns the text assigned to the specified environment string—that is, the text following the equal sign (=) in the environment-string table for that environment variable. If the string in Expression cannot be found in the environment-string table, a zero-length string ("") is returned.

If Expression contains an integer, the string occupying that numeric position in the environment-string table is returned. In this case, Environ returns all of the text, including the name of the environment variable. If there is no environment string in the specified position, Environ returns a zero-length string.

Security noteSecurity Note

The Environ function requires environment permission, which might affect its execution in partial-trust situations. For more information, see SecurityPermission and Code Access Permissions.

This example uses the Environ function to supply the entry number and length of the PATH statement from the environment-string table.

Sub tenv()
    Dim envString As String 
    Dim found As Boolean = False 
    Dim index As Integer = 1
    Dim pathLength As Integer 
    Dim message As String

    envString = Environ(index)
    While Not found And (envString <> "")
        If (envString.Substring(0, 5) = "Path=") Then
            found = True 
        Else
            index += 1
            envString = Environ(index)
        End If 
    End While 

    If found Then
        pathLength = Environ("PATH").Length
        message = "PATH entry = " & index & " and length = " & pathLength
    Else
        message = "No PATH environment variable exists." 
    End If

    MsgBox(message)
End Sub

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.