VB6Parse / Library / Environment / environ_dollar

VB6 Library Reference

Environ$ Function

Returns the string value associated with an environment variable.

Syntax

Environ$(envstring)
Environ$(number)

Parameters

Return Value

Returns a String containing the text assigned to the specified environment variable. If the environment variable doesn't exist, returns an empty string.

Remarks

The Environ$ function returns the string assigned to the specified environment variable from the operating system's environment-string table. This function cannot be used on the left side of an assignment statement.

When using a numeric argument, Environ$ returns the string that occupies that numeric position in the environment table. In this case, Environ$ returns all the text including the equal sign (=). If there's no environment string at the specified position, Environ$ returns a zero-length string.

When using a string argument, if the environment variable doesn't exist, a zero-length string is returned.

Typical Uses

Example 1: Getting System Path

Dim systemPath As String
systemPath = Environ$("PATH")

Example 2: Getting Temp Directory

Dim tempDir As String
tempDir = Environ$("TEMP")

Example 3: Getting User Name

Dim userName As String
userName = Environ$("USERNAME")

Example 4: Iterating Environment Variables

Dim i As Integer
Dim envVar As String
i = 1
Do
envVar = Environ$(i)
If envVar <> "" Then Debug.Print envVar
i = i + 1
Loop While envVar <> ""

Common Usage Patterns

Getting Application Data Path

Dim appDataPath As String
appDataPath = Environ$("APPDATA")
If appDataPath <> "" Then
appDataPath = appDataPath & "\MyApp\"
End If

Getting User Profile Directory

Dim userProfile As String
userProfile = Environ$("USERPROFILE")
configFile = userProfile & "\config.ini"

Checking for Development Environment

Dim devMode As Boolean
devMode = (Environ$("DEV_MODE") = "1")
If devMode Then
Debug.Print "Running in development mode"
End If

Building Full Path with Temp Directory

Dim tempFile As String
tempFile = Environ$("TEMP") & "\tempdata.tmp"
Open tempFile For Output As #1

Getting System Drive

Dim systemDrive As String
systemDrive = Environ$("SystemDrive")
logPath = systemDrive & "\Logs\app.log"

Listing All Environment Variables

Dim idx As Integer
Dim envEntry As String
For idx = 1 To 255
envEntry = Environ$(idx)
If envEntry = "" Then Exit For
List1.AddItem envEntry
Next idx

Cross-Platform Path Separator

Dim pathSep As String
If Environ$("OS") Like "Windows*" Then
pathSep = "\"
Else
pathSep = "/"
End If

Getting Computer Name

Dim computerName As String
computerName = Environ$("COMPUTERNAME")
If computerName = "" Then computerName = Environ$("HOSTNAME")

Building Log File Path with User Name

Dim logFile As String
logFile = "C:\Logs\" & Environ$("USERNAME") & ".log"
Open logFile For Append As #1
Print #1, Now & " - User logged in"
Close #1

Checking if Variable Exists

Dim dbServer As String
dbServer = Environ$("DB_SERVER")
If dbServer = "" Then
dbServer = "localhost"  ' Default value
End If

Best Practices

  1. Always check if the returned value is empty before using it
  2. Use string argument form for better code readability
  3. Cache frequently accessed environment variables
  4. Be aware of case sensitivity on different platforms
  5. Avoid modifying environment variables from VB6 (use shell APIs instead)
  6. Use proper path combining (avoid double backslashes)
  7. Consider using GetEnvironmentVariable API for more control
  8. Remember that environment variables persist only for the process lifetime
  9. Use constants for commonly used environment variable names
  10. Validate paths returned from environment variables before using them

Performance Considerations

Platform Differences

Platform Notes
Windows 95/98 Limited environment space (may fail with many variables)
Windows NT/2000/XP Larger environment space, more reliable
Windows Vista+ User and system environment variables separated
Wine/Linux May return different variables, case sensitivity differs

Common Environment Variables

Variable Description
PATH System search path for executables
TEMP or TMP Temporary files directory
APPDATA Application data folder (Windows)
USERPROFILE User's home directory (Windows)
USERNAME Current user's login name
COMPUTERNAME Computer's network name
SystemDrive Drive letter of system installation
SystemRoot Windows installation directory
HOMEDRIVE User's home drive letter
HOMEPATH User's home directory path

Common Pitfalls

Limitations

← Back to Environment | View all functions