EnvironmentVariableTarget Enum

Definition

指定在 Set 或 Get 操作中存储或检索环境变量的位置。Specifies the location where an environment variable is stored or retrieved in a set or get operation.

public enum class EnvironmentVariableTarget
public enum EnvironmentVariableTarget
[System.Runtime.InteropServices.ComVisible(true)]
public enum EnvironmentVariableTarget
type EnvironmentVariableTarget = 
Public Enum EnvironmentVariableTarget
Inheritance
EnvironmentVariableTarget
Attributes

Fields

Machine 2

在 Windows 操作系统注册表的 HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment 项中存储或从其中检索环境变量。The environment variable is stored or retrieved from the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment key in the Windows operating system registry. 此值应仅用于在 Windows 系统上运行的 .NET 实现。This value should be used on .NET implementations running on Windows systems only.

Process 0

环境变量存储在与当前进程关联的环境块中,或者从其中检索。The environment variable is stored or retrieved from the environment block associated with the current process.

User 1

在 Windows 操作系统注册表的 HKEY_CURRENT_USER\Environment 项中存储或从其中检索环境变量。The environment variable is stored or retrieved from the HKEY_CURRENT_USER\Environment key in the Windows operating system registry. 此值应仅用于在 Windows 系统上运行的 .NET 实现。This value should be used on .NET implementations running on Windows systems only.

Examples

下面的示例在创建、检索和删除环境变量的方法中使用 EnvironmentVariableTarget 枚举。The following example uses the EnvironmentVariableTarget enumeration in methods that create, retrieve, and delete environment variables. 该示例的输出显示,在未指定 EnvironmentVariableTarget 值的情况下存储和检索的环境变量存储在与当前进程(EnvironmentVariableTarget.Process)关联的环境块中。The output from the example shows that environmnent variables stored and retrieved without specifying a EnvironmentVariableTarget value are stored in the environment block associated with the current process (EnvironmentVariableTarget.Process). 基于 Unix 的系统的示例输出还表明,尝试使用 EnvironmentVariableTarget.Process 以外的值来定义环境变量将被忽略。The example output from Unix-based systems also shows that attempts to define an environment variable with a value other than EnvironmentVariableTarget.Process is ignored.

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...

Remarks

EnvironmentVariableTarget 枚举由 Environment.SetEnvironmentVariableEnvironment.GetEnvironmentVariableEnvironment.GetEnvironmentVariables 方法的某些重载用于指定在其中存储或检索环境变量的名称和值的位置或目标。The EnvironmentVariableTarget enumeration is used by certain overloads of the Environment.SetEnvironmentVariable, Environment.GetEnvironmentVariable, and Environment.GetEnvironmentVariables methods to specify the location, or target, where the name and value of an environment variable is stored or retrieved.

目标可以是以下三个位置之一:The target can be one of three locations:

  • 与当前进程关联的环境块(EnvironmentVariableTarget.Process)。The environment block associated with the current process (EnvironmentVariableTarget.Process).

    用户在设置操作中创建环境变量。The user creates the environment variable in a set operation. 进程终止后,操作系统将销毁该进程中的环境变量。When the process terminates, the operating system destroys the environment variable in that process.

  • 为与当前用户(EnvironmentVariableTarget.User)关联的环境变量保留的 Windows 操作系统注册表项。The Windows operating system registry key reserved for environment variables associated with the current user (EnvironmentVariableTarget.User).

    在 Windows 系统中,当用户在设置操作中创建环境变量时,操作系统将环境变量存储在系统注册表中,而不是在当前进程中。On Windows systems, when the user creates the environment variable in a set operation, the operating system stores the environment variable in the system registry, but not in the current process. 如果用户启动新进程,操作系统会将环境变量从注册表复制到该进程。If the user starts a new process, the operating system copies the environment variable from the registry to that process. 进程终止后,操作系统将销毁该进程中的环境变量。When the process terminates, the operating system destroys the environment variable in that process. 但是,注册表中的环境变量将一直持续,直到用户以编程方式或通过操作系统工具删除它。However, the environment variable in the registry persists until the user removes it programmatically or by means of an operating system tool.

    在基于 Unix 的系统上,尝试使用 EnvironmentVariable.User 创建环境变量不起作用,尝试使用 EnvironmentVariable.User 检索环境变量时,会返回 null (在中C#)或 Nothing (在 Visual Basic 中)。On Unix-based systems, an attempt to create an enviroment variable with EnvironmentVariable.User has no effect, and an attempt to retrieve an enviroment variable using EnvironmentVariable.User returns null (in C#) or Nothing (in Visual Basic).

  • 为与本地计算机上的所有用户(EnvironmentVariableTarget.Machine)关联的环境变量保留的注册表项。The registry key reserved for environment variables associated with all users on the local machine (EnvironmentVariableTarget.Machine).

    当用户在设置操作中创建环境变量时,操作系统将环境变量存储在系统注册表中,而不是存储在当前进程中。When a user creates the environment variable in a set operation, the operating system stores the environment variable in the system registry, but not in the current process. 如果本地计算机上的任何用户启动新进程,操作系统会将环境变量从注册表复制到该进程。If any user on the local machine starts a new process, the operating system copies the environment variable from the registry to that process. 进程终止后,操作系统将销毁该进程中的环境变量。When the process terminates, the operating system destroys the environment variable in that process. 但是,注册表中的环境变量将一直持续,直到用户以编程方式或通过操作系统工具删除它。However, the environment variable in the registry persists until a user removes it programmatically or by means of an operating system tool.

    在基于 Unix 的系统上,尝试使用 EnvironmentVariable.Machine 创建环境变量不起作用,尝试使用 EnvironmentVariable.Machine 检索环境变量时,会返回 null (在中C#)或 Nothing (在 Visual Basic 中)。On Unix-based systems, an attempt to create an enviroment variable with EnvironmentVariable.Machine has no effect, and an attempt to retrieve an enviroment variable using EnvironmentVariable.Machine returns null (in C#) or Nothing (in Visual Basic).

Applies to