|
|
Title | Set an environment variable |
Description | This example shows how to set an environment variable in Visual Basic 6. |
Keywords | environment variable, SetEnvironmentVariable, environ |
Categories | Software Engineering, Windows |
|
|
When it starts, this program loops through the environment variables displaying them.
When you click the Start Program button, it uses the SetEnvironmentVariable API function to set an environment variable. It then executes another instance of itself. That program will see the new variable.
|
|
' Display all environment variables.
Private Sub Form_Load()
Dim i As Integer
Dim txt As String
i = 1
Do
If Environ(i) = "" Then Exit Do
txt = txt & Environ(i) & vbCrLf
i = i + 1
Loop
txtVariables.Text = txt
End Sub
Private Sub cmdStartProgram_Click()
Dim file_path As String
' Set an environment variable.
SetEnvironmentVariable txtVariable.Text, txtValue.Text
' Run a new instance of this program.
file_path = App.Path
If Right$(file_path, 1) <> "\" Then file_path = _
file_path & "\"
file_path = file_path & "Project1.exe"
Shell file_path
End Sub
|
|
Note that the environment variable is only set for the program that set it and any otehr programs that start from its session. It will not be visible to other programs started independently.
|
|
|
|
|
|