EnvironmentVariableTarget 列舉

定義

指定在 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
繼承
EnvironmentVariableTarget
屬性

欄位

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.

範例

下列範例會在建立、取得和刪除環境變數的方法中使用 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...

備註

Environment.SetEnvironmentVariableEnvironment.GetEnvironmentVariableEnvironment.GetEnvironmentVariables 方法的特定多載會使用 EnvironmentVariableTarget 列舉來指定位置或目標,其中會儲存或抓取環境變數的名稱和值。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.

  • 為與目前使用者相關聯的環境變數保留的 Windows 作業系統登錄機碼(EnvironmentVariableTarget.User)。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).

適用於