Name
Environ Function
Class
Microsoft.VisualBasic.Interaction
Syntax
Environ(expression)-
expression(required; String, or a numeric expression) If
expressionis a string, it must be the name of the required environment variable; ifexpressionis numeric, it must be the 1-based ordinal number of the environment variable within the environment table.
Return Value
A String containing the text assigned to
expression
Description
Returns the value assigned to an operating-system environment variable
Rules at a Glance
A zero-length string (“”) is returned if
expressiondoes not exist in the operating system’s environment-string table or if there is no environment string in the position specified byexpression.expressioncan be either a string or a numeric expression; that is, you can specify one or the other, but not both.
Example
Public Module modMain
Public Structure env
Dim strVarName As String
Dim strValue As String
End Structure
Public Sub Main( )
Dim intCtr, intPos As Integer
Dim strRetVal As String
Dim udtEnv As env
intCtr = 1
Do
strRetVal = Environ(intCtr)
If strRetVal <> "" Then
intPos = InStr(1, strRetVal, "=")
udtEnv.strVarName = Left(strRetVal, intPos - 1)
udtEnv.strValue = Mid(strRetVal, intPos + 1)
Console.Writeline(udtEnv.strVarName & ": " & udtEnv.strValue)
Else
Exit Do
End If
intCtr = intCtr + 1
Loop
End Sub
End ModuleProgramming Tips and Gotchas
If
expressionis numeric, both the name and the value of the variable are returned. An equal sign (=) is used to separate them. For ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access