Interaction.Environ Method

Definition

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

Overloads

Environ(String)

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

Environ(Int32)

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

Environ(String)

Source:
Interaction.vb
Source:
Interaction.vb
Source:
Interaction.vb

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

public:
 static System::String ^ Environ(System::String ^ Expression);
public static string Environ (string? Expression);
public static string Environ (string Expression);
static member Environ : string -> string
Public Function Environ (Expression As String) As String

Parameters

Expression
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.

Returns

The string associated with an operating-system environment variable.

Exceptions

Expression is missing.

Examples

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

Remarks

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.

Important

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

See also

Applies to

Environ(Int32)

Source:
Interaction.vb
Source:
Interaction.vb
Source:
Interaction.vb

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

public:
 static System::String ^ Environ(int Expression);
public static string Environ (int Expression);
static member Environ : int -> string
Public Function Environ (Expression As Integer) As String

Parameters

Expression
Int32

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.

Returns

The string associated with an operating-system environment variable.

Exceptions

Expression is missing.

Examples

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

Remarks

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.

Important

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

See also

Applies to