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

定义

检索所有环境变量名及其值。Retrieves all environment variable names and their values.

重载

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

从当前进程检索所有环境变量名及其值。Retrieves all environment variable names and their values from the current process.

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

从当前进程或者从当前用户或本地计算机的 Windows 操作系统注册表项检索所有环境变量名及其值。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()

从当前进程检索所有环境变量名及其值。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

返回

包含所有环境变量名及其值的字典;如果找不到任何环境变量,则返回空字典。A dictionary that contains all environment variable names and their values; otherwise, an empty dictionary if no environment variables are found.

异常

调用方没有执行此操作所需的权限。The caller does not have the required permission to perform this operation.

缓冲区内存不足。The buffer is out of memory.

示例

下面的示例演示GetEnvironmentVariables方法。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.

注解

环境变量的名称和值作为键值对存储在返回IDictionary的中。The names and values for the environment variables are stored as key-value pairs in the returned IDictionary.

在 Windows 系统上On Windows systems

在 Windows 系统上, GetEnvironmentVariables此方法返回以下环境变量:On Windows systems, the GetEnvironmentVariables method returns the following environment variables:

在 macOS 和 Linux 系统上On macOS and Linux systems

在 MacOS 和 Linux 上, GetEnvironmentVariables方法会检索从dotnet启动进程的父进程dotnet中继承的或在进程本身的作用域内定义的所有环境变量的名称和值。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. dotnet进程结束后, 后面的后一种环境变量将停止存在。Once the dotnet process ends, these latter environment variables cease to exist.

在基于 Unix 的系统上运行的 .NET Core 不支持每计算机或每用户环境变量。.NET Core running on Unix-based systems does not support per-machine or per-user environment variables.

安全性

EnvironmentPermission
用于读取环境变量的名称和值。for the ability to read the names and values of environment variables. 关联的枚举:ReadAssociated enumeration: Read

另请参阅

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

从当前进程或者从当前用户或本地计算机的 Windows 操作系统注册表项检索所有环境变量名及其值。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

参数

target
EnvironmentVariableTarget EnvironmentVariableTarget EnvironmentVariableTarget EnvironmentVariableTarget

EnvironmentVariableTarget 值之一。One of the EnvironmentVariableTarget values. 在基于 Unix 的系统上运行的 .NET Core 仅支持 Only is supported on .NET Core running on Unix-based systems.

返回

包含 target 参数所指定的源中所有环境变量名及其值的字典;否则,如果找不到任何环境变量,则返回空字典。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.

异常

调用方不具有针对 target 的指定值执行此操作的所需权限。The caller does not have the required permission to perform this operation for the specified value of target.

target 包含非法值。target contains an illegal value.

示例

下面的示例为EnvironmentVariableTarget.ProcessEnvironmentVariableTarget.UserMachine目标创建环境变量, 检查操作系统注册表是否包含用户和计算机环境变量, 然后删除环境变化.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. 由于基于 Unix 的系统上的 .net 不支持每用户和每台计算机的环境变量, 因此SetEnvironmentVariable(String, String)只有SetEnvironmentVariable(String, String, EnvironmentVariableTarget)和值EnvironmentVariableTarget.Process才能成功地将环境变量存储到进程环境块。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...

注解

环境变量的名称和值作为键/值对存储在返回IDictionary的对象中。The names and values of the environment variables are stored as key/value pairs in the returned IDictionary object.

在 Windows 系统上On Windows systems

在 Windows 系统上, target参数指定源是否为当前进程、当前用户的注册表项或本地计算机的注册表项。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.

在 macOS 和 Linux 系统上On macOS and Linux systems

在 macOS 和 Linux 上, 只target支持EnvironmentVariableTarget.Process值。On macOS and Linux, only a target value of EnvironmentVariableTarget.Process is supported. 基于进程的环境变量是从用于启动dotnet进程的父进程 (通常是 shell) 继承的, 或在dotnet进程本身的作用域内定义的。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. Dotnet 进程结束后, 后面的后一种环境变量将停止存在。Once the dotnet process ends, these latter environment variables cease to exist.

不支持每计算机和每用户环境变量。Per-machine and per-user environment variables are not supported. target 值或EnvironmentVariableTarget.User返回空数组。 EnvironmentVariableTarget.MachineA target value of EnvironmentVariableTarget.Machine or EnvironmentVariableTarget.User returns an empty array.

安全性

EnvironmentPermission
如果target是 (关联的Process枚举: Read), 则可以读取环境变量的名称和值。for the ability to read the names and values of environment variables if target is Process (Associated enumeration: Read), oror 如果target为或User Unrestricted(关联的枚举:), 则表示完全访问环境变量。 Machinefor full access to environment variables if target is User or Machine (Associated enumeration: Unrestricted).

另请参阅

适用于