EnvironmentVariableTarget EnvironmentVariableTarget EnvironmentVariableTarget EnvironmentVariableTarget Enum

定義

環境変数の格納場所を指定します。Specifies the location where an environment variable is stored or retrieved in a set or get operation.

public enum class EnvironmentVariableTarget
[System.Runtime.InteropServices.ComVisible(true)]
public enum EnvironmentVariableTarget
type EnvironmentVariableTarget = 
Public Enum EnvironmentVariableTarget
継承
EnvironmentVariableTargetEnvironmentVariableTargetEnvironmentVariableTargetEnvironmentVariableTarget
属性

フィールド

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

Process Process Process Process 0

環境変数は、現在のプロセスに関連付けられた環境ブロックから取得されます。The environment variable is stored or retrieved from the environment block associated with the current process.

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

次のコード例に示します、EnvironmentVariableTarget作成、変更、および環境変数を削除するメソッドで列挙します。The following code example demonstrates the EnvironmentVariableTarget enumeration in methods that create, modify, and delete environment variables.

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

注釈

EnvironmentVariableTarget列挙の特定のオーバー ロードによって使用されます、 Environment.SetEnvironmentVariableEnvironment.GetEnvironmentVariable、およびEnvironment.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.

ターゲットには、3 つの場所のいずれかを指定できます。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).

    ユーザーは、設定操作で、環境変数を作成するとき、オペレーティング システムは、現在のプロセスではなく、システム レジストリで環境変数を格納します。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.

  • ローカル コンピューター上のすべてのユーザーに関連付けられた環境変数の予約済みのレジストリ キー (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.

適用対象