Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Environment.GetEnvironmentVariables Method ()

Retrieves all environment variable names and their values from the current process.

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

public static IDictionary GetEnvironmentVariables ()
public static IDictionary GetEnvironmentVariables ()
public static function GetEnvironmentVariables () : IDictionary
Not applicable.

Return Value

An IDictionary that contains all environment variable names and their values; otherwise, an empty dictionary if no environment variables are found.
Exception typeCondition

SecurityException

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

OutOfMemoryException

The buffer is out of memory.

The names and values for the environment variables are stored as key-value pairs in the returned IDictionary.

The following code example demonstrates the GetEnvironmentVariables method.

// Sample for the Environment.GetEnvironmentVariables method
using System;
using System.Collections;

class Sample 
{
    public static void Main() 
    {
    Console.WriteLine();
    Console.WriteLine("GetEnvironmentVariables: ");
    IDictionary	environmentVariables = Environment.GetEnvironmentVariables();
    foreach (DictionaryEntry de in environmentVariables)
        {
        Console.WriteLine("  {0} = {1}", de.Key, de.Value);
        }
    }
}
/*
This example produces the following results:
(Any result that is lengthy, specific to the machine on which this sample was tested, or reveals information that should remain secure, has been omitted and marked "!---OMITTED---!".)

GetEnvironmentVariables: 
  !---OMITTED---!
*/

// Sample for the Environment.GetEnvironmentVariables method
import System.*;
import System.Collections.*;

class Sample
{
    public static void main(String[] args)
    {
        Console.WriteLine();
        Console.WriteLine("GetEnvironmentVariables: ");
        IDictionary environmentVariables = Environment.
            GetEnvironmentVariables();
        DictionaryEntry de; 
        IEnumerator enumObj = environmentVariables.GetEnumerator();
        while (enumObj.MoveNext()) {
            de = (DictionaryEntry)enumObj.get_Current();
            Console.WriteLine("  {0} = {1}", de.get_Key(), de.get_Value());
        }
    } //main
} //Sample
/*
This example produces the following results:
(Any result that is lengthy, specific to the machine on which this sample 
was tested, or reveals information that should remain secure,
has been omitted and marked "!---OMITTED---!".)

GetEnvironmentVariables: 
  !---OMITTED---!
*/

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
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.