One of the most common uses of the environment is to update the Path, the list of places searched by Windows for executable programs. For example, in the articles about using VBScript for system administration, I demonstrated the special BAT file that comes with Visual Studio, SDKVARS.BAT, to update the Path variable in the environment.
"Batch" files (text files consisting of DOS commands) are one of the most common ways to updating the environment and SDKVARS.BAT is a great example of a short one.
But since batch files represent the past (and really aren't VB programming anyway) let's move on to writing code for environment variables with a real programming language.
There are two basic ways to access the environment in Visual Basic. The first is to use yet another convenience provided by the My namespace", the GetEnvironmentVariable.
Here's how to get the test environment variable shown earlier.
Debug.Write("Value in A_TestVar: " & _
My.Application.GetEnvironmentVariable("A_TestVar"))
This puts the following message into the Immediate window:
Value in A_TestVar: This is a test
If you want to get the entire Windows system Path, use this code:
Debug.WriteLine(Environment.GetEnvironmentVariable("path").ToString)
This writes your current system Path to the Immediate window:
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\ ... and so forth
One problem that you might have using the My namespace GetEnvironmentVariable code is knowing exactly what the name of the environment variable you want to "Get" is. If, for some reason, the variable isn't found in the environment, an ArgumentException error is thrown. There's a way to get the name of the variable too: the Environ function. But it takes a few more lines of code. This code will display all of the environment variables in the Immediate window!
Dim EnvVarCount As Integer
Dim EnvVariable As String
Dim EnvName As String
Dim EnvValue As String
For EnvVarCount = 1 To 255
EnvVariable = Environ(EnvVarCount)
If (EnvVariable = "") Then Exit For
EnvName = Split(EnvVariable, "=")(0)
EnvValue = Split(EnvVariable, "=")(1)
Debug.Write(EnvVarCount.ToString & vbCrLf)
Debug.Write(EnvName & vbCrLf)
Debug.Write(EnvValue & vbCrLf)
Next 'counter
... And, as a bonus, it takes advantage of Dr. Peter Ingerman's insight using the Split function documented in the article, Amazing Splits.
The second way to access the environment is to use the .NET 2.0 Environment namespace. Before this you had to use the SetEnvironmentVariable Windows API call. Since working with Win API calls is significantly harder in VB.NET, this was a real improvement! In this example, I add my own string to the environment.
Dim EnvName As String = "AboutVB"
Dim EnvValue As String = "Grrrreat!"
Environment.SetEnvironmentVariable(EnvName, EnvValue)
Shell("CMD.EXE")
The last line opens a Command Prompt window so you can check that the value is actually there using the DOS Set command.
(Thanks to Robert C Taubert for pointing out that, like the SetEnvironmentVariable Windows API call, the SetEnvironmentVariable method in the Environment namespace doesn't write to the registry so the value disappears again as soon as the process completes. If you want the value to be permanent, you have to write the value to the registry.
VB.NET also provides the Registry namespace. (Thanks to Jerry Baldwin for pointing this out. I'm getting lots of help on this article!) While you can read the registry with both Windows API calls and methods in the Registry namespace, writing to it is another matter entirely since the registry is actively controlling your Windows session at the same time you're trying to update it. Since this article isn't about the registry, let's just let the topic go with the note that if you decide to write this kind of program, you will probably learn why you have to reboot your computer when you update the registry.

