Environment.GetEnvironmentVariables Method

Definition

Retrieves all environment variable names and their values.

Overloads

GetEnvironmentVariables()

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

GetEnvironmentVariables(EnvironmentVariableTarget)

Retrieves all environment variable names and their values from the current process, or from the Windows operating system registry key for the current user or local machine.

GetEnvironmentVariables()

Source:
Environment.Variables.Windows.cs
Source:
Environment.Variables.Windows.cs
Source:
Environment.Variables.Windows.cs

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

C#
public static System.Collections.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

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

The buffer is out of memory.

Examples

The following example demonstrates the GetEnvironmentVariables method.

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

class Sample
{
    public static void Main()
    {
       Console.WriteLine();
       Console.WriteLine("GetEnvironmentVariables: ");
       foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
           Console.WriteLine("  {0} = {1}", de.Key, de.Value);
    }
}
// Output from the example is not shown, since it is:
//    Lengthy.
//    Specific to the machine on which the example is run.
//    May reveal information that should remain secure.

Remarks

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

On Windows systems

On Windows systems, the GetEnvironmentVariables method returns the following environment variables:

On Unix-like systems

On Unix-like systems, the GetEnvironmentVariables method retrieves the name and value of all environment variables that are inherited from the parent process that launched the dotnet process or that are defined within the scope of the dotnet process itself. Once the dotnet process ends, these latter environment variables cease to exist.

.NET running on Unix-like systems does not support per-machine or per-user environment variables.

Note

On Unix-like systems, in-process environment modifications made by native libraries aren't seen by managed callers.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

GetEnvironmentVariables(EnvironmentVariableTarget)

Source:
Environment.cs
Source:
Environment.cs
Source:
Environment.cs

Retrieves all environment variable names and their values from the current process, or from the Windows operating system registry key for the current user or local machine.

C#
public static System.Collections.IDictionary GetEnvironmentVariables(EnvironmentVariableTarget target);

Parameters

target
EnvironmentVariableTarget

One of the EnvironmentVariableTarget values. Only Process is supported on .NET running on Unix-like systems.

Returns

A dictionary that contains all environment variable names and their values from the source specified by the target parameter; otherwise, an empty dictionary if no environment variables are found.

Exceptions

The caller does not have the required permission to perform this operation for the specified value of target.

target contains an illegal value.

Examples

The following example creates environment variables for the EnvironmentVariableTarget.Process, EnvironmentVariableTarget.User, and Machine targets, checks whether the operating system registry contains the user and machine environment variables, then deletes the environment variables. Because .NET on Unix-like systems does not support per-user and per-machine environment variables, only SetEnvironmentVariable(String, String) and SetEnvironmentVariable(String, String, EnvironmentVariableTarget) with a value of EnvironmentVariableTarget.Process successfully store an environment variable to the process environment block.

C#
using System;
using System.Collections;
using Microsoft.Win32;

class Sample
{
    public static void Main()
    {
        // Environment variable names for default, process, user, and machine targets.
        string defaultEnvVar = nameof(defaultEnvVar);
        string processEnvVar = nameof(processEnvVar);
        string userEnvVar = nameof(userEnvVar);
        string machineEnvVar = nameof(machineEnvVar);

        string dft = nameof(dft);
        string process = nameof(process);
        string user = nameof(user);
        string machine = nameof(machine);

        // Set the environment variable for each target.
        Console.WriteLine("Setting environment variables for each target...\n");
        // The default target (the current process).
        Environment.SetEnvironmentVariable(defaultEnvVar, dft);
        // The current process.
        Environment.SetEnvironmentVariable(processEnvVar, process,
                                           EnvironmentVariableTarget.Process);
        // The current user.
        Environment.SetEnvironmentVariable(userEnvVar, user,
                                           EnvironmentVariableTarget.User);
        // The local machine.
        Environment.SetEnvironmentVariable(machineEnvVar, machine,
                                           EnvironmentVariableTarget.Machine);

        // Define an array of environment variables.
        string[] envVars = { defaultEnvVar,processEnvVar, userEnvVar, machineEnvVar };

        // Try to get the environment variables from each target.
        // The default (no specified target).
        Console.WriteLine("Retrieving environment variables from the default target:");
        foreach (var envVar in envVars)
        {
          var value = Environment.GetEnvironmentVariable(envVar) ?? "(none)";
          Console.WriteLine($"   {envVar}: {value}");
        }
        // The process block.
        Console.WriteLine("\nRetrieving environment variables from the Process target:");
        foreach (var envVar in envVars)
        {
          var value = Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.Process) ?? "(none)";
          Console.WriteLine($"   {envVar}: {value}");
        }
        // The user block.
        Console.WriteLine("\nRetrieving environment variables from the User target:");
        foreach (var envVar in envVars)
        {
          var value = Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.User) ?? "(none)";
          Console.WriteLine($"   {envVar}: {value}");
        }
        // The machine block.
        Console.WriteLine("\nRetrieving environment variables from the Machine target:");
        foreach (var envVar in envVars)
        {
          var value = Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.Machine) ?? "(none)";
          Console.WriteLine($"   {envVar}: {value}");
        }

        // Delete the environment variable for each target.
        Console.WriteLine("\nDeleting environment variables for each target...\n");
        // The default target (the current process).
        Environment.SetEnvironmentVariable(defaultEnvVar, null);
        // The current process.
        Environment.SetEnvironmentVariable(processEnvVar, null,
                                           EnvironmentVariableTarget.Process);
        // The current user.
        Environment.SetEnvironmentVariable(userEnvVar, null,
                                           EnvironmentVariableTarget.User);
        // The local machine.
        Environment.SetEnvironmentVariable(machineEnvVar, null,
                                           EnvironmentVariableTarget.Machine);
    }
}
// The example displays the following output if run on a Windows system:
//      Setting environment variables for each target...
//
//      Retrieving environment variables from the default target:
//        defaultEnvVar: dft
//        processEnvVar: process
//        userEnvVar: user
//        machineEnvVar: (none)
//
//      Retrieving environment variables from the Process target:
//        defaultEnvVar: dft
//        processEnvVar: process
//        userEnvVar: user
//        machineEnvVar: (none)
//
//      Retrieving environment variables from the User target:
//        defaultEnvVar: (none)
//        processEnvVar: (none)
//        userEnvVar: user
//        machineEnvVar: (none)
//
//      Retrieving environment variables from the Machine target:
//        defaultEnvVar: (none)
//        processEnvVar: (none)
//        userEnvVar: (none)
//        machineEnvVar: machine
//
//      Deleting environment variables for each target...
//
// The example displays the following output if run on a Unix-based system:
//
//      Setting environment variables for each target...
//
//      Retrieving environment variables from the default target:
//        defaultEnvVar: dft
//        processEnvVar: process
//        userEnvVar: (none)
//        machineEnvVar: (none)
//
//      Retrieving environment variables from the Process target:
//        defaultEnvVar: dft
//        processEnvVar: process
//        userEnvVar: (none)
//        machineEnvVar: (none)
//
//      Retrieving environment variables from the User target:
//        defaultEnvVar: (none)
//        processEnvVar: (none)
//        userEnvVar: (none)
//        machineEnvVar: (none)
//
//      Retrieving environment variables from the Machine target:
//        defaultEnvVar: (none)
//        processEnvVar: (none)
//        userEnvVar: (none)
//        machineEnvVar: (none)
//
//      Deleting environment variables for each target...

Remarks

The names and values of the environment variables are stored as key/value pairs in the returned IDictionary object.

On Windows systems

On Windows systems, the target parameter specifies whether the source is the current process, the registry key for the current user, or the registry key for the local machine.

On Unix-like systems

On Unix-like systems, only a target value of EnvironmentVariableTarget.Process is supported. Per-process environment variables are inherited from the parent process (typically the shell) used to launch the dotnet process or are defined within the scope of the dotnet process itself. Once the dotnet process ends, these latter environment variables cease to exist.

Per-machine and per-user environment variables aren't supported. A target value of EnvironmentVariableTarget.Machine or EnvironmentVariableTarget.User returns an empty array.

Note

On Unix-like systems, in-process environment modifications made by native libraries aren't seen by managed callers.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1