Retrieve Environment Variables

Problem

You want to retrieve information from a Windows environment variable (for example, to find out the computer name, username, logon server, and so on).

Solution

Use the GetEnvironmentVariable or GetEnvironmentVariables methods of the System.Environment class.

Discussion

The Windows operating system stores some commonly used information in environment variables. You can access this information by using the Environment.GetEnvironmentVariable method and supplying the name of the variable. The following code snippet uses this technique to retrieve the name of the current computer:

Dim ComputerName As String
ComputerName = Environment.GetEnvironmentVariable("COMPUTERNAME")

Applications can define and set their own environment ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.