Environ Function


Environ Function

Class

Microsoft.VisualBasic.Interaction

Syntax

     Dim result As String = Environ(expression) 


expression (required; String, or a numeric expression)

If expression is a string, it must be the name of an environment variable; if expression is numeric, it must be the 1-based ordinal number of the environment variable within the environment table.

Description

The Environ function returns the value assigned to an operating-system environment variable.

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

  • If expression is numeric, both the name and the value of the variable are returned. An equals sign (=) is used to separate them. For example, the function call Environ(1) might return the string "TEMP=C:\WINDOWS\TEMP."

  • Environment variables are defined through various system startup files and relevant registry entries.

Example

     Public Sub LoadEnvironmentStrings(  )        ' ----- Store all environment strings internally.        Dim environmentEntries As New Collection        Dim oneEntry As String        Dim counter As Integer        Dim parts As String(  )        ' ----- Scan through each valid environment variable.        counter = 1        Do           ' ----- Get the next entry.           oneEntry = Environ(counter)           If (oneEntry = "") Then Exit Do           counter = counter + 1           ' ----- Get the name and value parts.           parts = Split(oneEntry, "=")           ' ----- Store the variable in the collection.           If (UBound(parts) = 0) Then              environmentEntries.Add("(undefined)", parts(0))           Else              environmentEntries.Add(parts(1), parts(0))           End If        Loop        MsgBox("Loaded " & environmentEntries.Count & " variable(s).")     End Sub 

Version Differences

  • In VB 6, the Environ function retrieved environmental variables and their values only from the environment-string table. In .NET, the function retrieves values from both the environment-string table and the system registry.

  • In VB 6, the function could be called using either the envString named argument (if the argument was the name of an environment variable) or the number named argument (if the number represented the ordinal position of the variable in the environment table). VB.NET replaces these with a single named argument, expression.

  • Visual Basic 2005 adds the new My.Application.GetEnvironmentVariable method, which provides related functionality.




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net