How to Read Environment Variables in VB6
Environment variables are maintained by the Windows operating system. Environment variables can be used in scripts or Visual Basic programs to reference a standard directory that might be different from one machine to the next. For example, the "PATH" environment variable will likely be different on each computer. "SYSTEM ROOT" is another environment variable that is different for each operating system. There are also predefined environment variables such as "DATE" and "TIME." VB applications can read environment variables with the "Environ" function.
Instructions
-
-
1
Create a new Standard EXE Visual Basic project. A form named "Form1" is created by default.
-
2
Declare local variables in the "Form1" load event. You will need an integer, a boolean and a string to hold the value of the environment variable.
Dim i as Integer
Dim bFound as Boolean
Dim sEnvValue as String
-
-
3
Set the "i" variable equal to one. Next, read the environment variable at position one in the index and store it in "sEnvValue."
sEnvValue = Environ(i)
-
4
Loop through all the environment variables until you find the one you are searching for. Do this with a "While" statement. This code sets "bFound" to true if the "PATH" environment variable is located.
While Not bFound AND sEnvValue <> ""
If Ucase(Left(sEnvValue,5)) = "PATH=" Then
bFound = True
Else
i = i + 1
sEnvValue = Environ(i)
End If
Wend
The "Ucase" function converts "sEnvValue" to all uppercase because the comparison is case sensitive. If "PATH" is not found, read the next environment variable until we have read them all.
-
5
Check the value of "bFound" to determine what steps to perform next. We now have the value of the "PATH" environment variable located in "sEnvValue" and can parse it, display it, insert it into a table, write it to a file or any other actions you would normally take with a string variable.
-
1