Environment.GetEnvironmentVariables メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
すべての環境変数の名前と値を取得します。
オーバーロード
GetEnvironmentVariables() |
すべての環境変数の名前と値を現在のプロセスから取得します。 |
GetEnvironmentVariables(EnvironmentVariableTarget) |
現在のプロセスから、または Windows オペレーティング システムのレジストリ キー (現在のユーザー用に予約されているレジストリ キーまたはローカル コンピューター用に予約されているレジストリ キー) から、すべての環境変数の名前と値を取得します。 |
GetEnvironmentVariables()
すべての環境変数の名前と値を現在のプロセスから取得します。
public:
static System::Collections::IDictionary ^ GetEnvironmentVariables();
public static System.Collections.IDictionary GetEnvironmentVariables ();
static member GetEnvironmentVariables : unit -> System.Collections.IDictionary
Public Shared Function GetEnvironmentVariables () As IDictionary
戻り値
すべての環境変数の名前と値を保持するディクショナリ。環境変数が見つからなかった場合は空のディクショナリ。
例外
呼び出し元に、この操作を実行するために必要なアクセス許可がありません。
バッファーがメモリ不足です。
例
GetEnvironmentVariablesメソッドの例を次に示します。
using namespace System;
using namespace System::Collections;
int main()
{
Console::WriteLine( "GetEnvironmentVariables: " );
for each (DictionaryEntry^ de in Environment::GetEnvironmentVariables())
Console::WriteLine( " {0} = {1}", de->Key, de->Value );
}
// Output from the example is not shown, since it is:
// Lengthy.
// Specific to the machine on which the example is run.
// May reveal information that should remain secure.
// Sample for the Environment.GetEnvironmentVariables method
using System;
using System.Collections;
class Sample
{
public static void Main()
{
Console.WriteLine();
Console.WriteLine("GetEnvironmentVariables: ");
foreach (DictionaryEntry de in Environment.GetEnvironmentVariables())
Console.WriteLine(" {0} = {1}", de.Key, de.Value);
}
}
// Output from the example is not shown, since it is:
// Lengthy.
// Specific to the machine on which the example is run.
// May reveal information that should remain secure.
// Sample for the Environment.GetEnvironmentVariables method
open System
open System.Collections
printfn "\nGetEnvironmentVariables: "
for de in Environment.GetEnvironmentVariables() do
let de = de :?> DictionaryEntry
printfn $" {de.Key} = {de.Value}"
// Output from the example is not shown, since it is:
// Lengthy.
// Specific to the machine on which the example is run.
// May reveal information that should remain secure.
' Sample for the Environment.GetEnvironmentVariables method
Imports System.Collections
Class Sample
Public Shared Sub Main()
Console.WriteLine("GetEnvironmentVariables: ")
For Each de As DictionaryEntry In Environment.GetEnvironmentVariables()
Console.WriteLine(" {0} = {1}", de.Key, de.Value)
Next
End Sub
End Class
' Output from the example is not shown, since it is:
' Lengthy.
' Specific to the machine on which the example is run.
' May reveal information that should remain secure.
注釈
環境変数の名前と値は、返された IDictionaryキーと値のペアとして格納されます。
Windows システムの場合
Windows システムでは、メソッドはGetEnvironmentVariables
次の環境変数を返します。
プロセスの作成時に定義されたすべてのマシンごとの環境変数とその値。
プロセスの作成時に定義されたすべてのユーザーごとの環境変数とその値。
プロセスの実行中に .NET アプリケーションの起動元またはプロセス ブロックに追加された親プロセスから継承された変数。 環境変数は、プロセスの実行中に、メソッドまたはSetEnvironmentVariable(String, String, EnvironmentVariableTarget)
target
メソッドの値EnvironmentVariableTarget.Processを指定して呼び出SetEnvironmentVariable(String, String)すことによって追加されます。
macOSおよび Linux システムの場合
MacOS および Linux では、このメソッドは、 GetEnvironmentVariables
プロセスを起動 dotnet
した親プロセスから継承されたすべての環境変数、またはプロセス自体のスコープ内で定義されているすべての環境変数の名前と値を dotnet
取得します。 プロセスが dotnet
終了すると、これらの後者の環境変数は存在しなくなります。
Unix ベースのシステムで実行されている .NET では、マシンごとまたはユーザーごとの環境変数はサポートされていません。
こちらもご覧ください
適用対象
GetEnvironmentVariables(EnvironmentVariableTarget)
現在のプロセスから、または Windows オペレーティング システムのレジストリ キー (現在のユーザー用に予約されているレジストリ キーまたはローカル コンピューター用に予約されているレジストリ キー) から、すべての環境変数の名前と値を取得します。
public:
static System::Collections::IDictionary ^ GetEnvironmentVariables(EnvironmentVariableTarget target);
public static System.Collections.IDictionary GetEnvironmentVariables (EnvironmentVariableTarget target);
static member GetEnvironmentVariables : EnvironmentVariableTarget -> System.Collections.IDictionary
Public Shared Function GetEnvironmentVariables (target As EnvironmentVariableTarget) As IDictionary
パラメーター
- target
- EnvironmentVariableTarget
EnvironmentVariableTarget 値のいずれか 1 つ。 Unix ベースのシステムで実行されている .NET でのみ Process サポートされます。
戻り値
target
パラメーターに指定したソースから取得された、すべての環境変数の名前と値を保持するディクショナリ。環境変数が見つからない場合は空のディクショナリ。
例外
呼び出し元には、target
の指定された値にこの操作を実行するために必要なアクセス許可がありません。
target
に正しくない値が含まれています。
例
次の例では、ターゲットの環境変数 EnvironmentVariableTarget.Processを EnvironmentVariableTarget.User作成し Machine 、オペレーティング システム レジストリにユーザー環境変数とマシン環境変数が含まれているかどうかを確認してから、環境変数を削除します。 Unix ベースのシステムの .NET では、ユーザーごとの環境変数とマシンごとの環境変数はサポートされていないため、環境変数をEnvironmentVariableTarget.Processプロセス環境ブロックに正常に格納する値のみをSetEnvironmentVariable(String, String)SetEnvironmentVariable(String, String, EnvironmentVariableTarget)使用します。
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...
注釈
環境変数の名前と値は、返された IDictionary オブジェクトにキーと値のペアとして格納されます。
Windows システムの場合
Windows システムでは、target
ソースが現在のプロセスか、現在のユーザーのレジストリ キーか、ローカル コンピューターのレジストリ キーかを指定します。
macOSおよび Linux システムの場合
macOSと Linux では、値EnvironmentVariableTarget.Processのみがtarget
サポートされます。 プロセスごとの環境変数は、プロセスの起動 dotnet
に使用される親プロセス (通常はシェル) から継承されるか、プロセス自体のスコープ内で dotnet
定義されます。 dotnet プロセスが終了すると、これらの後者の環境変数は存在しなくなります。
マシンごとの環境変数とユーザーごとの環境変数はサポートされていません。 target
空の配列のEnvironmentVariableTarget.Machine値またはEnvironmentVariableTarget.User値を返します。