Anyone who has written CMD knows about environment variables. They let you easily get information that differs from one computer to another, such as the user name. In CMD, using an environment variable is simple: you just write it in the %environment variable name% format. In a .NET program, though, it isn't that simple. You need to use the Environ function.

Reading Environment Variables in .NET

In .NET, you read system variables with the Environ function. The function is simple to use, and its format is as follows:

Environ("環境變數名稱")

P.S. Note that the environment variable name should be treated as a string, not a variable, so wrap it in double quotes.

Example (console application):

Module Module1
Sub Main()
    Console.WriteLine("CPU核心數:" & Environ("NUMBER_OF_PROCESSORS"))
    Console.ReadLine()
End Sub

End Module

Common Windows Environment Variable Names

Below are some common environment variable names. They're handy for getting information that differs from one computer to another, such as the number of CPU cores or the user name.
SystemRootLocation of the operating system root directoryC:WINDOWS
ComSpecLocation of CMDC:WINDOWSsystem32cmd.exe
NUMBER_OF_PROCESSORSNumber of CPU cores4
TEMPLocation of the system temp folderC:WINDOWSTEMP
HOMEDRIVEDrive where the current system is locatedC:
HOMEPATHShows the folder location of the current useruseradministrator
OSName of the operating system in useWindows NT
ProgramFilesLocation of the ProgramFiles folderC:ProgramFiles
USERNAMECurrent user nameAdministrator
COMPUTERNAMEGet the computer nameLINROEX-PC