System.Environment.GetEnvironmentVariables Method

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

Syntax

public static IDictionary GetEnvironmentVariables ()

Returns

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

Exceptions

TypeReason
System.Security.SecurityExceptionThe caller does not have the required permission.

Remarks

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

Permissions

Example

The following example prints the names and values of all environment variables defined in the environment.

C# Example

using System;
using System.Collections;

class EnvTest:Object {
  public static void Main() {
    Console.WriteLine("Environment Variables");
    IDictionary envars =
        Environment.GetEnvironmentVariables();
    IDictionaryEnumerator varEnumerator =
        envars.GetEnumerator();
    while(varEnumerator.MoveNext() != false) {
      Console.WriteLine("{0}={1}", 
                        varEnumerator.Key,
                        varEnumerator.Value);
    }
  }
}
   

The output will vary depending on your system.

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0