Environment.GetEnvironmentVariable Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Načte hodnotu proměnné prostředí.
Přetížení
| Name | Description |
|---|---|
| GetEnvironmentVariable(String) |
Načte hodnotu proměnné prostředí z aktuálního procesu. |
| GetEnvironmentVariable(String, EnvironmentVariableTarget) |
Načte hodnotu proměnné prostředí z aktuálního procesu nebo z klíče registru operačního systému Windows pro aktuálního uživatele nebo místního počítače. |
GetEnvironmentVariable(String)
- Zdroj:
- Environment.cs
- Zdroj:
- Environment.cs
- Zdroj:
- Environment.cs
- Zdroj:
- Environment.cs
- Zdroj:
- Environment.cs
Načte hodnotu proměnné prostředí z aktuálního procesu.
public:
static System::String ^ GetEnvironmentVariable(System::String ^ variable);
public static string GetEnvironmentVariable(string variable);
public static string? GetEnvironmentVariable(string variable);
static member GetEnvironmentVariable : string -> string
Public Shared Function GetEnvironmentVariable (variable As String) As String
Parametry
- variable
- String
Název proměnné prostředí.
Návraty
Hodnota proměnné prostředí určené parametrem variable, nebo null pokud proměnná prostředí nebyla nalezena.
Výjimky
variable je null.
Volající nemá požadovaná oprávnění k provedení této operace.
Příklady
Následující příklad používá metodu GetEnvironmentVariable k načtení proměnné prostředí windir, která obsahuje cestu k adresáři Windows.
using System;
using System.IO;
public class Example
{
public static void Main()
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
// Change the directory to %WINDIR%
Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir");
DirectoryInfo info = new DirectoryInfo(".");
Console.WriteLine("Directory Info: " + info.FullName);
}
else
{
Console.WriteLine("This example runs on Windows only.");
}
}
}
// The example displays output like the following on a .NET implementation running on Windows:
// Directory Info: C:\windows
// The example displays the following output on a .NET implementation on Unix-based systems:
// This example runs on Windows only.
open System
open System.IO
if Environment.OSVersion.Platform = PlatformID.Win32NT then
// Change the directory to %WINDIR%
Environment.CurrentDirectory <- Environment.GetEnvironmentVariable "windir"
let info = DirectoryInfo "."
printfn $"Directory Info: {info.FullName}"
else
printfn "This example runs on Windows only."
// The example displays output like the following on a .NET implementation running on Windows:
// Directory Info: C:\windows
// The example displays the following output on a .NET implementation on Unix-based systems:
// This example runs on Windows only.
Imports System.IO
Module Example
Public Sub Main()
If Environment.OSVersion.Platform = PlatformID.Win32NT Then
' Change the directory to %WINDIR%
Environment.CurrentDirectory = Environment.GetEnvironmentVariable("windir")
Dim info As New DirectoryInfo(".")
Console.WriteLine("Directory Info: " + info.FullName)
Else
Console.WriteLine("This example runs on Windows only.")
End If
End Sub
End Module
' The example displays output like the following on a .NET implementation running on Windows:
' Directory Info: C:\windows
' The example displays the following output on a .NET implementation on Unix-based systems:
' This example runs on Windows only.
Následující příklad se pokusí načíst hodnotu proměnné prostředí pojmenované Test1 z bloku procesního prostředí. Pokud proměnná neexistuje, příklad ji vytvoří a načte její hodnotu. Příklad zobrazí hodnotu proměnné. Pokud příklad vytvořil proměnnou, volá také metodu GetEnvironmentVariables(EnvironmentVariableTarget) s každým členem EnvironmentVariableTarget výčtu, aby bylo možné zjistit, že proměnnou lze načíst pouze z aktuálního bloku prostředí procesu. A nakonec, pokud příklad vytvořil proměnnou, odstraní ji.
using System;
public class Example
{
public static void Main()
{
string value;
bool toDelete = false;
// Check whether the environment variable exists.
value = Environment.GetEnvironmentVariable("Test1");
// If necessary, create it.
if (value == null)
{
Environment.SetEnvironmentVariable("Test1", "Value1");
toDelete = true;
// Now retrieve it.
value = Environment.GetEnvironmentVariable("Test1");
}
// Display the value.
Console.WriteLine($"Test1: {value}\n");
// Confirm that the value can only be retrieved from the process
// environment block if running on a Windows system.
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
Console.WriteLine("Attempting to retrieve Test1 from:");
foreach (EnvironmentVariableTarget enumValue in
Enum.GetValues(typeof(EnvironmentVariableTarget))) {
value = Environment.GetEnvironmentVariable("Test1", enumValue);
Console.WriteLine($" {enumValue}: {(value != null ? "found" : "not found")}");
}
Console.WriteLine();
}
// If we've created it, now delete it.
if (toDelete) {
Environment.SetEnvironmentVariable("Test1", null);
// Confirm the deletion.
if (Environment.GetEnvironmentVariable("Test1") == null)
Console.WriteLine("Test1 has been deleted.");
}
}
}
// The example displays the following output if run on a Windows system:
// Test1: Value1
//
// Attempting to retrieve Test1 from:
// Process: found
// User: not found
// Machine: not found
//
// Test1 has been deleted.
//
// The example displays the following output if run on a Unix-based system:
// Test1: Value1
//
// Test1 has been deleted.
module Example
open System
let mutable toDelete = false
// Check whether the environment variable exists.
let value =
let v = Environment.GetEnvironmentVariable "Test1"
// If necessary, create it.
if isNull v then
Environment.SetEnvironmentVariable("Test1", "Value1")
toDelete <- true
Environment.GetEnvironmentVariable "Test1"
else
v
// Display the value.
printfn $"Test1: {value}\n"
// Confirm that the value can only be retrieved from the process
// environment block if running on a Windows system.
if Environment.OSVersion.Platform = PlatformID.Win32NT then
printfn "Attempting to retrieve Test1 from:"
for enumValue in Enum.GetValues typeof<EnvironmentVariableTarget> do
let value = Environment.GetEnvironmentVariable("Test1", enumValue :?> EnvironmentVariableTarget)
printfn $""" {enumValue}: {if value <> null then "found" else "not found"}"""
printfn ""
// If we've created it, now delete it.
if toDelete then
Environment.SetEnvironmentVariable("Test1", null)
// Confirm the deletion.
if Environment.GetEnvironmentVariable "Test1" |> isNull then
printfn "Test1 has been deleted."
// The example displays the following output if run on a Windows system:
// Test1: Value1
//
// Attempting to retrieve Test1 from:
// Process: found
// User: not found
// Machine: not found
//
// Test1 has been deleted.
//
// The example displays the following output if run on a Unix-based system:
// Test1: Value1
//
// Test1 has been deleted.
Module Example
Public Sub Main()
Dim value As String
Dim toDelete As Boolean = False
' Check whether the environment variable exists.
value = Environment.GetEnvironmentVariable("Test1")
' If necessary, create it.
If value Is Nothing Then
Environment.SetEnvironmentVariable("Test1", "Value1")
toDelete = True
' Now retrieve it.
value = Environment.GetEnvironmentVariable("Test1")
End If
' Display the value.
Console.WriteLine($"Test1: {value}")
Console.WriteLine()
' Confirm that the value can only be retrieved from the process
' environment block if running on a Windows system.
If Environment.OSVersion.Platform = PlatformID.Win32NT Then
Console.WriteLine("Attempting to retrieve Test1 from:")
For Each enumValue As EnvironmentVariableTarget In
[Enum].GetValues(GetType(EnvironmentVariableTarget))
value = Environment.GetEnvironmentVariable("Test1", enumValue)
Console.WriteLine($" {enumValue}: {If(value IsNot Nothing, "found", "not found")}")
Next
Console.WriteLine()
End If
' If we've created it, now delete it.
If toDelete Then
Environment.SetEnvironmentVariable("Test1", Nothing)
' Confirm the deletion.
If Environment.GetEnvironmentVariable("Test1") = Nothing Then
Console.WriteLine("Test1 has been deleted.")
End If
End If
End Sub
End Module
' The example displays the following output if run on a Windows system:
' Test1: Value1
'
' Attempting to retrieve Test1 from:
' Process: found
' User: not found
' Machine: not found
'
' Test1 has been deleted.
'
' The example displays the following output if run on a Unix-based system:
' Test1: Value1
'
' Test1 has been deleted.
Poznámky
Metoda GetEnvironmentVariable načte hodnotu proměnné prostředí z aktuálního procesu.
Názvy proměnných prostředí rozlišují velikost písmen v unixových systémech, ale v systému Windows nikoliv.
Note
Úpravy procesu prostředí provedené nativními knihovnami nejsou viditelné pro volající spravovaného kódu. Naopak takové změny provedené spravovanými volajícími nejsou patrné nativními knihovnami.
metoda GetEnvironmentVariable(String)
Metoda GetEnvironmentVariable(String) načte proměnnou prostředí pouze z bloku prostředí aktuálního procesu. Je to ekvivalentní jako zavolat metodu GetEnvironmentVariable(String, EnvironmentVariableTarget) s hodnotou targetEnvironmentVariableTarget.Process.
Pokud chcete načíst všechny proměnné prostředí spolu s jejich hodnotami, zavolejte metodu GetEnvironmentVariables .
V systémech Windows
V systémech Windows blok prostředí aktuálního procesu zahrnuje:
Všechny proměnné prostředí, které mu poskytuje nadřazený proces, který ho vytvořil. Například aplikace .NET spuštěná z okna konzoly dědí všechny proměnné prostředí okna konzoly.
Pokud neexistuje žádný nadřazený proces, použijí se proměnné prostředí pro jednotlivé počítače a uživatele. Například nové okno konzoly obsahuje všechny proměnné prostředí pro jednotlivé počítače a uživatele definované v době, kdy byla spuštěna.
Všechny proměnné přidané do bloku procesu, zatímco proces běží voláním SetEnvironmentVariable(String, String) metody nebo SetEnvironmentVariable(String, String, EnvironmentVariableTarget) metody s
targethodnotou EnvironmentVariableTarget.Process. Tyto proměnné prostředí se zachovají do ukončení aplikace .NET.
Pokud jsou proměnné prostředí vytvořeny po spuštění procesu, můžete tuto metodu použít k načtení pouze těch proměnných, které byly vytvořeny voláním SetEnvironmentVariable(String, String) metody nebo SetEnvironmentVariable(String, String, EnvironmentVariableTarget) metody s target hodnotou .EnvironmentVariableTarget.Process.
Na systémech podobných Unixu
Blok prostředí aktuálního procesu v Unixových systémech obsahuje následující proměnné prostředí:
Všechny proměnné prostředí, které mu poskytuje nadřazený proces, který ho vytvořil. Pro aplikace .NET spuštěné z prostředí to zahrnuje všechny proměnné prostředí definované v prostředí.
Všechny proměnné přidané do bloku procesu, zatímco proces běží voláním SetEnvironmentVariable(String, String) metody nebo SetEnvironmentVariable(String, String, EnvironmentVariableTarget) metody s
targethodnotou EnvironmentVariableTarget.Process. Tyto proměnné prostředí se zachovají do ukončení aplikace .NET.
.NET v systémech Unix-like nepodporuje proměnné prostředí pro jednotlivé počítače ani uživatele.
metoda GetEnvironmentVariable(String, EnvironmentVariableTarget)
Pokud chcete načíst všechny proměnné prostředí spolu s jejich hodnotami, zavolejte metodu GetEnvironmentVariables .
V systémech Windows
V systému Windows určuje parametr, target zda je proměnná prostředí načtena z aktuálního procesu nebo z klíče registru operačního systému Windows pro aktuálního uživatele nebo místního počítače. Všechny proměnné prostředí pro jednotlivé uživatele a počítače se automaticky zkopírují do bloku prostředí aktuálního procesu, stejně jako všechny ostatní proměnné prostředí, které jsou k dispozici pro nadřazený proces, který vytvořil proces .NET. Proměnné prostředí, které jsou přidány pouze do bloku prostředí aktuálního procesu voláním metody SetEnvironmentVariable(String, String) nebo metody SetEnvironmentVariable(String, String, EnvironmentVariableTarget) s hodnotou target, trvají pouze po dobu trvání procesu.
Na systémech podobných Unixu
V systémech Unix podporuje GetEnvironmentVariable(String, EnvironmentVariableTarget) metoda target pouze hodnotu EnvironmentVariableTarget.Process . Volání s target hodnotou EnvironmentVariableTarget.Machine nebo EnvironmentVariableTarget.User nejsou podporována a vrací null.
Proměnné prostředí pro jednotlivé procesy jsou:
Ty zděděné z nadřazeného procesu, obvykle prostředí používané k vyvolání
dotnet.exenebo spuštění aplikace .NET.Ty, které jsou definovány voláním SetEnvironmentVariable(String, String) metody nebo SetEnvironmentVariable(String, String, EnvironmentVariableTarget) metody s
targethodnotou EnvironmentVariableTarget.Process. Tyto proměnné prostředí se uchovávají pouze do doby, než procesdotnetnebo aplikace .NET skončí.
Viz také
Platí pro
GetEnvironmentVariable(String, EnvironmentVariableTarget)
- Zdroj:
- Environment.cs
- Zdroj:
- Environment.cs
- Zdroj:
- Environment.cs
- Zdroj:
- Environment.cs
- Zdroj:
- Environment.cs
Načte hodnotu proměnné prostředí z aktuálního procesu nebo z klíče registru operačního systému Windows pro aktuálního uživatele nebo místního počítače.
public:
static System::String ^ GetEnvironmentVariable(System::String ^ variable, EnvironmentVariableTarget target);
public static string? GetEnvironmentVariable(string variable, EnvironmentVariableTarget target);
public static string GetEnvironmentVariable(string variable, EnvironmentVariableTarget target);
static member GetEnvironmentVariable : string * EnvironmentVariableTarget -> string
Public Shared Function GetEnvironmentVariable (variable As String, target As EnvironmentVariableTarget) As String
Parametry
- variable
- String
Název proměnné prostředí.
- target
- EnvironmentVariableTarget
Jedna z EnvironmentVariableTarget hodnot. Na .NET spuštěných v systémech unixových systémů se podporuje pouze Process.
Návraty
Hodnota proměnné prostředí určená variable parametry a target parametry nebo null pokud proměnná prostředí nebyla nalezena.
Výjimky
variable je null.
target není platná EnvironmentVariableTarget hodnota.
Volající nemá požadovaná oprávnění k provedení této operace.
Příklady
Následující příklad vytvoří proměnné prostředí pro EnvironmentVariableTarget.Process, EnvironmentVariableTarget.Usera Machine cíle, zkontroluje, zda registr operačního systému obsahuje proměnné prostředí uživatele a počítače a pak odstraní proměnné prostředí. Protože .NET v systémech Unix-like nepodporuje proměnné prostředí pro jednotlivé uživatele a počítače, pouze SetEnvironmentVariable(String, String) a SetEnvironmentVariable(String, String, EnvironmentVariableTarget) s hodnotou EnvironmentVariableTarget.Process úspěšně uložit proměnnou prostředí do bloku procesního prostředí.
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...
Poznámky
Metoda GetEnvironmentVariable načte hodnotu proměnné prostředí z aktuálního procesu.
Názvy proměnných prostředí rozlišují velikost písmen v unixových systémech, ale v systému Windows nikoliv.
Note
Úpravy procesu prostředí provedené nativními knihovnami nejsou viditelné pro volající spravovaného kódu. Naopak takové změny provedené spravovanými volajícími nejsou patrné nativními knihovnami.
metoda GetEnvironmentVariable(String)
Metoda GetEnvironmentVariable(String) načte proměnnou prostředí pouze z bloku prostředí aktuálního procesu. Je to ekvivalentní jako zavolat metodu GetEnvironmentVariable(String, EnvironmentVariableTarget) s hodnotou targetEnvironmentVariableTarget.Process.
Pokud chcete načíst všechny proměnné prostředí spolu s jejich hodnotami, zavolejte metodu GetEnvironmentVariables .
V systémech Windows
V systémech Windows blok prostředí aktuálního procesu zahrnuje:
Všechny proměnné prostředí, které mu poskytuje nadřazený proces, který ho vytvořil. Například aplikace .NET spuštěná z okna konzoly dědí všechny proměnné prostředí okna konzoly.
Pokud neexistuje žádný nadřazený proces, použijí se proměnné prostředí pro jednotlivé počítače a uživatele. Například nové okno konzoly obsahuje všechny proměnné prostředí pro jednotlivé počítače a uživatele definované v době, kdy byla spuštěna.
Všechny proměnné přidané do bloku procesu, zatímco proces běží voláním SetEnvironmentVariable(String, String) metody nebo SetEnvironmentVariable(String, String, EnvironmentVariableTarget) metody s
targethodnotou EnvironmentVariableTarget.Process. Tyto proměnné prostředí se zachovají do ukončení aplikace .NET.
Pokud jsou proměnné prostředí vytvořeny po spuštění procesu, můžete tuto metodu použít k načtení pouze těch proměnných, které byly vytvořeny voláním SetEnvironmentVariable(String, String) metody nebo SetEnvironmentVariable(String, String, EnvironmentVariableTarget) metody s target hodnotou .EnvironmentVariableTarget.Process.
Na systémech podobných Unixu
Blok prostředí aktuálního procesu v Unixových systémech obsahuje následující proměnné prostředí:
Všechny proměnné prostředí, které mu poskytuje nadřazený proces, který ho vytvořil. Pro aplikace .NET spuštěné z prostředí to zahrnuje všechny proměnné prostředí definované v prostředí.
Všechny proměnné přidané do bloku procesu, zatímco proces běží voláním SetEnvironmentVariable(String, String) metody nebo SetEnvironmentVariable(String, String, EnvironmentVariableTarget) metody s
targethodnotou EnvironmentVariableTarget.Process. Tyto proměnné prostředí se zachovají do ukončení aplikace .NET.
.NET v systémech Unix-like nepodporuje proměnné prostředí pro jednotlivé počítače ani uživatele.
metoda GetEnvironmentVariable(String, EnvironmentVariableTarget)
Pokud chcete načíst všechny proměnné prostředí spolu s jejich hodnotami, zavolejte metodu GetEnvironmentVariables .
V systémech Windows
V systému Windows určuje parametr, target zda je proměnná prostředí načtena z aktuálního procesu nebo z klíče registru operačního systému Windows pro aktuálního uživatele nebo místního počítače. Všechny proměnné prostředí pro jednotlivé uživatele a počítače se automaticky zkopírují do bloku prostředí aktuálního procesu, stejně jako všechny ostatní proměnné prostředí, které jsou k dispozici pro nadřazený proces, který vytvořil proces .NET. Proměnné prostředí, které jsou přidány pouze do bloku prostředí aktuálního procesu voláním metody SetEnvironmentVariable(String, String) nebo metody SetEnvironmentVariable(String, String, EnvironmentVariableTarget) s hodnotou target, trvají pouze po dobu trvání procesu.
Na systémech podobných Unixu
V systémech Unix podporuje GetEnvironmentVariable(String, EnvironmentVariableTarget) metoda target pouze hodnotu EnvironmentVariableTarget.Process . Volání s target hodnotou EnvironmentVariableTarget.Machine nebo EnvironmentVariableTarget.User nejsou podporována a vrací null.
Proměnné prostředí pro jednotlivé procesy jsou:
Ty zděděné z nadřazeného procesu, obvykle prostředí používané k vyvolání
dotnet.exenebo spuštění aplikace .NET.Ty, které jsou definovány voláním SetEnvironmentVariable(String, String) metody nebo SetEnvironmentVariable(String, String, EnvironmentVariableTarget) metody s
targethodnotou EnvironmentVariableTarget.Process. Tyto proměnné prostředí se uchovávají pouze do doby, než procesdotnetnebo aplikace .NET skončí.
Viz také
- SetEnvironmentVariable(String, String, EnvironmentVariableTarget)
- GetEnvironmentVariables()
- EnvironmentVariableTarget