O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

Environ Function

Class

Microsoft.VisualBasic.Interaction

Syntax

Environ(expression)
expression (required; 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 them. For ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required