Name

Environ Function

Class

Microsoft.VisualBasic.Interaction

Syntax

Environ(expression)
expression

Use: Required

Data Type: String, or a numeric expression

If expression is a string, it must be the name of the required environment variable; if expression is 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 expression does not exist in the operating system’s environment-string table or if there is no environment string in the position specified by expression.

  • expression can 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 Module

Programming Tips and Gotchas

  • If expression is numeric, both the name and the value of the variable are returned. An equal sign (=) is used to separate ...

Get VB .NET Language in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.