This documentation is archived and is not being maintained.

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

Parameters

variable

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

ArgumentNullException

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

SecurityException

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(".");
    lock(info)
    {      
    Console.WriteLine("Directory Info:   "+info.FullName);
    }

// Change the directory to %WINDIR%                            
Environment.set_CurrentDirectory(Environment.GetEnvironmentVariable(
    "windir"));

DirectoryInfo info = new DirectoryInfo(".");

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

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Show: