Environment.GetEnvironmentVariables Environment.GetEnvironmentVariables Environment.GetEnvironmentVariables Environment.GetEnvironmentVariables Method

Definition

Ruft die Namen und Werte aller Umgebungsvariablen ab.Retrieves all environment variable names and their values.

Überlädt

GetEnvironmentVariables() GetEnvironmentVariables() GetEnvironmentVariables() GetEnvironmentVariables()

Ruft alle Namen und Werte der Umgebungsvariablen vom aktuellen Prozess ab.Retrieves all environment variable names and their values from the current process.

GetEnvironmentVariables(EnvironmentVariableTarget) GetEnvironmentVariables(EnvironmentVariableTarget) GetEnvironmentVariables(EnvironmentVariableTarget) GetEnvironmentVariables(EnvironmentVariableTarget)

Ruft die Namen und Werte aller Umgebungsvariablen vom aktuellen Prozess oder aus dem Registrierungsschlüssel des Windows-Betriebssystems für den aktuellen Benutzer oder den lokalen Computer ab.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() GetEnvironmentVariables() GetEnvironmentVariables() GetEnvironmentVariables()

Ruft alle Namen und Werte der Umgebungsvariablen vom aktuellen Prozess ab.Retrieves all environment variable names and their values from the current process.

public:
 static System::Collections::IDictionary ^ GetEnvironmentVariables();
public static System.Collections.IDictionary GetEnvironmentVariables ();
static member GetEnvironmentVariables : unit -> System.Collections.IDictionary
Public Shared Function GetEnvironmentVariables () As IDictionary

Gibt zurück

Ein Wörterbuch, das die Namen und Werte aller Umgebungsvariablen enthält, andernfalls ein leeres Wörterbuch, wenn keine Umgebungsvariablen gefunden werden.A dictionary that contains all environment variable names and their values; otherwise, an empty dictionary if no environment variables are found.

Ausnahmen

Der Aufrufer hat nicht die erforderliche Berechtigung zum Ausführen dieses Vorgangs.The caller does not have the required permission to perform this operation.

Im Puffer ist nicht genügend Arbeitsspeicher.The buffer is out of memory.

Beispiele

Das folgende Beispiel veranschaulicht die GetEnvironmentVariables Methode.The following example demonstrates the GetEnvironmentVariables method.

using namespace System;
using namespace System::Collections;

int main()
{
   Console::WriteLine( "GetEnvironmentVariables: " );
   for each (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.
// 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.
' Sample for the Environment.GetEnvironmentVariables method
Imports System
Imports System.Collections

Class Sample
   Public Shared Sub Main()
      Console.WriteLine("GetEnvironmentVariables: ")
      For Each de As DictionaryEntry In Environment.GetEnvironmentVariables()
         Console.WriteLine("  {0} = {1}", de.Key, de.Value)
      Next 
   End Sub 
End Class 
' 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.

Hinweise

Die Namen und Werte für die Umgebungsvariablen werden als Schlüssel-Wert-Paare in der zurück IDictionarygegebenen gespeichert.The names and values for the environment variables are stored as key-value pairs in the returned IDictionary.

Auf Windows-SystemenOn Windows systems

Auf Windows-Systemen gibt GetEnvironmentVariables die-Methode die folgenden Umgebungsvariablen zurück:On Windows systems, the GetEnvironmentVariables method returns the following environment variables:

  • Alle Umgebungsvariablen pro Computer, die zum Zeitpunkt der Erstellung des Prozesses definiert sind, zusammen mit ihren Werten.All per-machine environment variables that are defined at the time the process is created, along with their values.

  • Alle Umgebungsvariablen pro Benutzer, die zum Zeitpunkt der Erstellung des Prozesses definiert sind, zusammen mit ihren Werten.All per-user environment variables that are defined at the time the process is created, along with their values.

  • Alle Variablen, die vom übergeordneten Prozess geerbt wurden, von dem aus die .NET-Anwendung gestartet oder dem Prozess Block hinzugefügt wurde, während der Prozess ausgeführt wird.Any variables inherited from the parent process from which the .NET application was launched or added to the process block while the process is running. Umgebungsvariablen werden hinzugefügt, während der Prozess ausgeführt wird, indem SetEnvironmentVariable(String, String) entweder die- SetEnvironmentVariable(String, String, EnvironmentVariableTarget) Methode oder die target - EnvironmentVariableTarget.ProcessMethode mit dem Wert aufgerufen wird.Environment variables are added while the process is running by calling either the SetEnvironmentVariable(String, String) method or the SetEnvironmentVariable(String, String, EnvironmentVariableTarget) method with a target value of EnvironmentVariableTarget.Process.

Auf macOS-und Linux-SystemenOn macOS and Linux systems

Unter MacOS und Linux Ruft die GetEnvironmentVariables -Methode den Namen und den Wert aller Umgebungsvariablen ab, die vom übergeordneten Prozess geerbt werden, der dotnet den Prozess gestartet hat oder die dotnet im Bereich des Prozesses selbst definiert werden.On MacOS and Linux, 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. Nachdem der dotnet Prozess beendet wurde, sind diese letzteren Umgebungsvariablen nicht mehr vorhanden.Once the dotnet process ends, these latter environment variables cease to exist.

.Net Core, das auf UNIX-basierten Systemen ausgeführt wird, unterstützt keine Umgebungsvariablen pro Computer oder pro Benutzer..NET Core running on Unix-based systems does not support per-machine or per-user environment variables.

Sicherheit

EnvironmentPermission
die Möglichkeit, die Namen und Werte von Umgebungsvariablen zu lesen.for the ability to read the names and values of environment variables. Zugehörige Enumeration:ReadAssociated enumeration: Read

Siehe auch

GetEnvironmentVariables(EnvironmentVariableTarget) GetEnvironmentVariables(EnvironmentVariableTarget) GetEnvironmentVariables(EnvironmentVariableTarget) GetEnvironmentVariables(EnvironmentVariableTarget)

Ruft die Namen und Werte aller Umgebungsvariablen vom aktuellen Prozess oder aus dem Registrierungsschlüssel des Windows-Betriebssystems für den aktuellen Benutzer oder den lokalen Computer ab.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.

public:
 static System::Collections::IDictionary ^ GetEnvironmentVariables(EnvironmentVariableTarget target);
public static System.Collections.IDictionary GetEnvironmentVariables (EnvironmentVariableTarget target);
static member GetEnvironmentVariables : EnvironmentVariableTarget -> System.Collections.IDictionary
Public Shared Function GetEnvironmentVariables (target As EnvironmentVariableTarget) As IDictionary

Parameter

target
EnvironmentVariableTarget EnvironmentVariableTarget EnvironmentVariableTarget EnvironmentVariableTarget

Einer der EnvironmentVariableTarget-Werte.One of the EnvironmentVariableTarget values. Nur wird in .NET Core bei der Ausführung auf Unix-basierten Systemen unterstützt.Only is supported on .NET Core running on Unix-based systems.

Gibt zurück

Ein Wörterbuch, das die Namen und Werte aller Umgebungsvariablen aus der durch den target-Parameter angegebenen Quelle enthält, andernfalls ein leeres Wörterbuch, wenn keine Umgebungsvariablen gefunden werden.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.

Ausnahmen

Der Aufrufer hat für den angegebenen Wert von target nicht die erforderliche Berechtigung zum Ausführen dieses Vorgangs.The caller does not have the required permission to perform this operation for the specified value of target.

target enthält einen unzulässigen Wert.target contains an illegal value.

Beispiele

Das folgende Beispiel erstellt die Umgebungsvariablen für die EnvironmentVariableTarget.Process, EnvironmentVariableTarget.User, und Machine ausgerichtet ist, überprüft, ob die Registrierung des Betriebssystems, die Benutzer und Computer-Umgebungsvariablen enthält, und löscht dann die Umgebungsvariablen.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. Da .net auf UNIX-basierten Systemen nicht pro Benutzer-und Computer spezifische Umgebungsvariablen unterstützt, werden nur SetEnvironmentVariable(String, String) und SetEnvironmentVariable(String, String, EnvironmentVariableTarget) mit dem Wert EnvironmentVariableTarget.Process erfolgreich eine Umgebungsvariable im Process Environment-Block gespeichert.Because .NET on Unix-based 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.

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...
Imports System.Collections
Imports Microsoft.Win32

Module Sample 
    Public Sub Main() 
        ' Environment variable names for default, process, user, and machine targets.
        Dim defaultEnvVar As String = NameOf(defaultEnvVar)
        Dim processEnvVar As String = NameOf(processEnvVar)
        Dim userEnvVar As String = NameOf(userEnvVar)
        Dim machineEnvVar As String = NameOf(machineEnvVar)

        Dim dft As String = NameOf(dft)
        Dim process As String = NameOf(process)
        Dim user As String = NameOf(user)
        Dim machine As String = NameOf(machine)

        ' Set the environment variable for each target.
        Console.WriteLine("Setting environment variables for each target...")
        ' 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)
        Console.WriteLine()

        ' Define an array of environment variables.
        Dim envVars As String() = { 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:")
        For Each envVar in envVars
          Dim value = Environment.GetEnvironmentVariable(envVar)
          Console.WriteLine($"   {envVar}: {If(value IsNot Nothing, value, "(none)")}")
        Next
        Console.WriteLine()
        ' The process block.
        Console.WriteLine("Retrieving environment variables from the Process target:")
        For Each envVar in envVars
          Dim value = Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.Process)
          Console.WriteLine($"   {envVar}: {If(value IsNot Nothing, value, "(none)")}")
        Next
        Console.WriteLine()
        ' The user block.
        Console.WriteLine("Retrieving environment variables from the User target:")
        For Each envVar in envVars
          Dim value = Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.User)
          Console.WriteLine($"   {envVar}: {value}")
        Next
        Console.WriteLine()
        ' The machine block.
        Console.WriteLine("Retrieving environment variables from the Machine target:")
        For Each envVar in envVars
          Dim value = Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.Machine)
          Console.WriteLine($"   {envVar}: {value}")
        Next
        Console.WriteLine()

        ' Delete the environment variable for each target.
        Console.WriteLine("Deleting environment variables for each target...")
        ' The default target (the current process).
        Environment.SetEnvironmentVariable(defaultEnvVar, Nothing)
        ' The current process.
        Environment.SetEnvironmentVariable(processEnvVar, Nothing, 
                                           EnvironmentVariableTarget.Process)
        ' The current user.
        Environment.SetEnvironmentVariable(userEnvVar, Nothing, 
                                           EnvironmentVariableTarget.User)
        ' The local machine.
        Environment.SetEnvironmentVariable(machineEnvVar, Nothing, 
                                           EnvironmentVariableTarget.Machine)
    End Sub
End Module
' 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...

Hinweise

Die Namen und Werte der Umgebungsvariablen werden als Schlüssel-Wert-Paare im zurückgegebenen IDictionary -Objekt gespeichert.The names and values of the environment variables are stored as key/value pairs in the returned IDictionary object.

Auf Windows-SystemenOn Windows systems

Auf Windows-Systemen gibt target der-Parameter an, ob die Quelle der aktuelle Prozess, der Registrierungsschlüssel für den aktuellen Benutzer oder der Registrierungsschlüssel für den lokalen Computer ist.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.

Auf macOS-und Linux-SystemenOn macOS and Linux systems

Unter macOS und Linux wird nur target der EnvironmentVariableTarget.Process Wert unterstützt.On macOS and Linux, only a target value of EnvironmentVariableTarget.Process is supported. Prozessspezifische Umgebungsvariablen werden von dem übergeordneten Prozess (in der Regel die Shell) geerbt, der dotnet zum Starten des Prozesses verwendet wird, oder werden dotnet innerhalb des Bereichs des Prozesses selbst definiert.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. Sobald der dotnet-Prozess beendet ist, sind diese letzten Umgebungsvariablen nicht mehr vorhanden.Once the dotnet process ends, these latter environment variables cease to exist.

Pro-Computer-und benutzerspezifische Umgebungsvariablen werden nicht unterstützt.Per-machine and per-user environment variables are not supported. Der target WertEnvironmentVariableTarget.Machine oderEnvironmentVariableTarget.User gibt ein leeres Array zurück.A target value of EnvironmentVariableTarget.Machine or EnvironmentVariableTarget.User returns an empty array.

Sicherheit

EnvironmentPermission
damit die Namen und Werte von Umgebungsvariablen gelesen werden können, wenn target ( Process zugeordnete Enumeration: Read) ist,for the ability to read the names and values of environment variables if target is Process (Associated enumeration: Read), oderor für den Vollzugriff auf Umgebungsvariablen target , User Wenn Machine oder ist (zugeordnete UnrestrictedEnumeration:).for full access to environment variables if target is User or Machine (Associated enumeration: Unrestricted).

Siehe auch

Gilt für: