Environ Function |
Microsoft.VisualBasic.Interaction
Environ( 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.
A String containing the text assigned to expression
Returns the value assigned to an operating-system environment variable
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.
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
If expression is numeric, both the name and the value of the variable are returned. An equal sign (=) is used to separate them. For example, the function call Environ(1) might return the string TEMP=C:\WINDOWS\TEMP .
If you retrieve environment variables and their values by ordinal position, the first variable is in position 1, not position 0.
Due to the flexibility offered , it is now accepted and recommended practice to use the registry for variables needed by your application, rather than the environment-string table.
Environment variables can be defined in a variety of ways, including by the AUTOEXEC.BAT and MSDOS.SYS files, as well as by the HKEY_LOCAL_ MACHINE\System\CurrentControlSet\Control\SessionManager\ Environment and HKEY_CURRENT_USER\Environment keys in the registry.
In VB 6, the Environ function retrieved environmental variables and their values only from the environment-string table. In VB.NET, the function retrieves values both from 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 .