Environ, Environ$ Functions |
Yes
Environ({envstring | number})
envstring
Key: Optional
Data Type: String
The name of the required environment variable.
number
Key: Optional
Data Type: Numeric expression
The ordinal number of the environment variable within the environment string table.
Environ returns a string containing the text assigned to envstring.
Returns the value assigned to an operating-system environment variable.
A zero-length string ("") is returned if envstring doesn't exist in the operating system's environment-string table, or if there is no environment string in the position specified by number.
envstring and number are mutually exclusive; that is, you can specify one or the other, but not both.
Private Type env strVarName As String strValue As String End Type Private Sub Form_Load() Dim intCtr As Integer, 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) Else Exit Do End If intCtr = intCtr + 1 Loop End Sub
If you use number to specify the environment variable, both the name and the value of the variable are returned. An equals sign (=) separates them. For example, the function call Environ(1) might return the string TEMP=C:WINDOWSTEMP.
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's 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_MACHINESystemCurrentControlSetControlSessionManagerEnvironment and HKEY_CURRENT_USEREnvironment keys in the registry. However, the Environ function doesn't recognize environment variables defined in the registry.