O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required