Environment.GetEnvironmentVariable Method (String)

Retrieves the value of an environment variable from the current process.

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

public static string GetEnvironmentVariable (
	string variable
public static String GetEnvironmentVariable (
	String variable
public static function GetEnvironmentVariable (
	variable : String
) : String



The name of the environment variable.

Return Value

The value of the environment variable specified by variable, or a null reference (Nothing in Visual Basic) if the environment variable is not found.

Exception typeCondition


variable is a null reference (Nothing in Visual Basic).


The caller does not have the required permission to perform this operation.

Environment variable names are not case-sensitive.

The following code example demonstrates the GetEnvironmentVariable method.

// Change the directory to %WINDIR%                            
Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir");        
DirectoryInfo info = new DirectoryInfo(".");
    Console.WriteLine("Directory Info:   "+info.FullName);

// Change the directory to %WINDIR%                            

DirectoryInfo info = new DirectoryInfo(".");

synchronized(info) {
    Console.WriteLine(("Directory Info:   " + info.get_FullName()));

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0