EnvironmentVariableTarget Wyliczenie

Definicja

Określa lokalizację, w której zmienna środowiskowa jest przechowywana lub pobierana w zestawie lub operacji pobierania.

public enum class EnvironmentVariableTarget
public enum EnvironmentVariableTarget
[System.Runtime.InteropServices.ComVisible(true)]
public enum EnvironmentVariableTarget
type EnvironmentVariableTarget = 
[<System.Runtime.InteropServices.ComVisible(true)>]
type EnvironmentVariableTarget = 
Public Enum EnvironmentVariableTarget
Dziedziczenie
EnvironmentVariableTarget
Atrybuty

Pola

Machine 2

Zmienna środowiskowa jest przechowywana lub pobierana z HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment klucza w rejestrze systemu operacyjnego Windows. Ta wartość powinna być używana tylko w implementacjach platformy .NET działających w systemach Windows.

Process 0

Zmienna środowiskowa jest przechowywana lub pobierana z bloku środowiska skojarzonego z bieżącym procesem.

User 1

Zmienna środowiskowa jest przechowywana lub pobierana z HKEY_CURRENT_USER\Environment klucza w rejestrze systemu operacyjnego Windows. Ta wartość powinna być używana tylko w implementacjach platformy .NET działających w systemach Windows.

Przykłady

W poniższym przykładzie użyto EnvironmentVariableTarget wyliczenia w metodach, które tworzą, pobierają i usuwają zmienne środowiskowe. Dane wyjściowe z przykładu pokazują, że zmienne środowiska przechowywane i pobierane bez określania wartości są przechowywane w bloku środowiska skojarzonym z bieżącym procesem EnvironmentVariableTarget (EnvironmentVariableTarget.Process). Przykładowe dane wyjściowe z systemów opartych na systemie Unix pokazują również, że próbuje zdefiniować zmienną środowiskową z wartością inną niż EnvironmentVariableTarget.Process jest ignorowana.

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

open System

// Environment variable names for default, process, user, and machine targets.
let rec defaultEnvVar = nameof defaultEnvVar
let rec processEnvVar = nameof processEnvVar
let rec userEnvVar = nameof userEnvVar
let rec machineEnvVar = nameof machineEnvVar

let rec dft = nameof dft
let rec proc = nameof proc
let rec user = nameof user
let rec machine = nameof machine

// Set the environment variable for each target.
printfn "Setting environment variables for each target...\n"
// The default target (the current process).
Environment.SetEnvironmentVariable(defaultEnvVar, dft)
// The current process.
Environment.SetEnvironmentVariable(processEnvVar, proc, EnvironmentVariableTarget.Process)
// The current user.
Environment.SetEnvironmentVariable(userEnvVar, user, EnvironmentVariableTarget.User)
// The local machine.
Environment.SetEnvironmentVariable(machineEnvVar, machine, EnvironmentVariableTarget.Machine)

// Define a list of environment variables.
let envVars = [ defaultEnvVar; processEnvVar; userEnvVar; machineEnvVar ]

// Try to get the environment variables from each target.
// The default (no specified target).
printfn "Retrieving environment variables from the default target:"
for envVar in envVars do
    let value = 
        match Environment.GetEnvironmentVariable envVar with
        | null -> "(none)"
        | v -> v
    printfn $"   {envVar}: {value}"

// The process block.
printfn "\nRetrieving environment variables from the Process target:"
for envVar in envVars do
    let value = 
        match Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.Process) with
        | null -> "(none)"
        | v -> v
    printfn $"   {envVar}: {value}"

// The user block.
printfn "\nRetrieving environment variables from the User target:"
for envVar in envVars do
    let value = 
        match Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.User) with
        | null -> "(none)"
        | v -> v
    printfn $"   {envVar}: {value}"

// The machine block.
printfn "\nRetrieving environment variables from the Machine target:"
for envVar in envVars do
    let value = 
        match Environment.GetEnvironmentVariable(envVar, EnvironmentVariableTarget.Machine) with
        | null -> "(none)"
        | v -> v
    printfn $"   {envVar}: {value}"

// Delete the environment variable for each target.
printfn "\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...

Uwagi

Wyliczenie EnvironmentVariableTarget jest używane przez pewne przeciążenia Environment.SetEnvironmentVariablemetod , Environment.GetEnvironmentVariablei Environment.GetEnvironmentVariables do określania lokalizacji lub miejsca docelowego, gdzie nazwa i wartość zmiennej środowiskowej są przechowywane lub pobierane.

Obiekt docelowy może być jedną z trzech lokalizacji:

  • Blok środowiska skojarzony z bieżącym procesem (EnvironmentVariableTarget.Process).

    Użytkownik tworzy zmienną środowiskową w operacji zestawu. Po zakończeniu procesu system operacyjny niszczy zmienną środowiskową w tym procesie.

  • Klucz rejestru systemu operacyjnego Windows zarezerwowany dla zmiennych środowiskowych skojarzonych z bieżącym użytkownikiem (EnvironmentVariableTarget.User).

    W systemach Windows, gdy użytkownik tworzy zmienną środowiskową w operacji zestawu, system operacyjny przechowuje zmienną środowiskową w rejestrze systemowym, ale nie w bieżącym procesie. Jeśli użytkownik rozpocznie nowy proces, system operacyjny kopiuje zmienną środowiskową z rejestru do tego procesu. Po zakończeniu procesu system operacyjny niszczy zmienną środowiskową w tym procesie. Jednak zmienna środowiskowa w rejestrze będzie się powtarzać, dopóki użytkownik nie usunie go programowo lub za pomocą narzędzia systemu operacyjnego.

    W systemach opartych na systemie Unix próba utworzenia zmiennej środowiskowej bez EnvironmentVariable.User żadnego wpływu i próba pobrania zmiennej środowiska przy użyciu EnvironmentVariable.User funkcji zwracanych null (w języku C#) lub Nothing (w Visual Basic).

  • Klucz rejestru zarezerwowany dla zmiennych środowiskowych skojarzonych ze wszystkimi użytkownikami na komputerze lokalnym (EnvironmentVariableTarget.Machine).

    Gdy użytkownik tworzy zmienną środowiskową w operacji zestawu, system operacyjny przechowuje zmienną środowiskową w rejestrze systemowym, ale nie w bieżącym procesie. Jeśli jakikolwiek użytkownik na komputerze lokalnym uruchamia nowy proces, system operacyjny kopiuje zmienną środowiskową z rejestru do tego procesu. Po zakończeniu procesu system operacyjny niszczy zmienną środowiskową w tym procesie. Jednak zmienna środowiskowa w rejestrze będzie się powtarzać, dopóki użytkownik nie usunie go programowo lub za pomocą narzędzia systemu operacyjnego.

    W systemach opartych na systemie Unix próba utworzenia zmiennej środowiskowej bez EnvironmentVariable.Machine żadnego wpływu i próba pobrania zmiennej środowiska przy użyciu EnvironmentVariable.Machine funkcji zwracanych null (w języku C#) lub Nothing (w Visual Basic).

Dotyczy