Environment.GetEnvironmentVariables メソッド

定義

すべての環境変数の名前と値を取得します。Retrieves all environment variable names and their values.

オーバーロード

GetEnvironmentVariables()

すべての環境変数の名前と値を現在のプロセスから取得します。Retrieves all environment variable names and their values from the current process.

GetEnvironmentVariables(EnvironmentVariableTarget)

現在のプロセスから、または Windows オペレーティング システムのレジストリ キー (現在のユーザー用に予約されているレジストリ キーまたはローカル コンピューター用に予約されているレジストリ キー) から、すべての環境変数の名前と値を取得します。Retrieves all environment variable names and their values from the current process, or from the Windows operating system registry key for the current user or local machine.

GetEnvironmentVariables()

すべての環境変数の名前と値を現在のプロセスから取得します。Retrieves all environment variable names and their values from the current process.

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

戻り値

すべての環境変数の名前と値を保持するディクショナリ。環境変数が見つからなかった場合は空のディクショナリ。A dictionary that contains all environment variable names and their values; otherwise, an empty dictionary if no environment variables are found.

例外

呼び出し元に、この操作を実行するために必要なアクセス許可がありません。The caller does not have the required permission to perform this operation.

バッファーがメモリ不足です。The buffer is out of memory.

次の例は、GetEnvironmentVariables メソッドを示しています。The following example demonstrates the GetEnvironmentVariables method.

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
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 にキーと値のペアとして格納されます。The names and values for the environment variables are stored as key-value pairs in the returned IDictionary.

Windows システムの場合On Windows systems

Windows システムでは、GetEnvironmentVariables メソッドは次の環境変数を返します。On Windows systems, the GetEnvironmentVariables method returns the following environment variables:

  • プロセスの作成時に定義されたすべてのコンピューターごとの環境変数とその値。All per-machine environment variables that are defined at the time the process is created, along with their values.

  • プロセスの作成時に定義されたすべてのユーザーごとの環境変数とその値。All per-user environment variables that are defined at the time the process is created, along with their values.

  • プロセスの実行中に .NET アプリケーションを起動またはプロセスブロックに追加した親プロセスから継承された変数。Any variables inherited from the parent process from which the .NET application was launched or added to the process block while the process is running. 環境変数は、プロセスの実行中に追加されます。そのためには、@no__t 0 のメソッド、または target の値が EnvironmentVariableTarget.Process の @no__t のいずれかのメソッドを呼び出します。Environment variables are added while the process is running by calling either the SetEnvironmentVariable(String, String) method or the SetEnvironmentVariable(String, String, EnvironmentVariableTarget) method with a target value of EnvironmentVariableTarget.Process.

MacOS および Linux システムの場合On macOS and Linux systems

MacOS および Linux では、GetEnvironmentVariables メソッドは、dotnet プロセスを起動した親プロセスから継承されたすべての環境変数の名前と値を取得します。また、dotnet プロセス自体のスコープ内で定義されています。On MacOS and Linux, the GetEnvironmentVariables method retrieves the name and value of all environment variables that are inherited from the parent process that launched the dotnet process or that are defined within the scope of the dotnet process itself. @No__t 0 プロセスが終了すると、これらの後者の環境変数は存在しなくなります。Once the dotnet process ends, these latter environment variables cease to exist.

Unix ベースのシステムで実行されている .NET Core は、コンピューターごとまたはユーザーごとの環境変数をサポートしていません。.NET Core running on Unix-based systems does not support per-machine or per-user environment variables.

セキュリティ

EnvironmentPermission
環境変数の名前と値を読み取る機能。for the ability to read the names and values of environment variables. 関連付けられた列挙型: ReadAssociated enumeration: Read

こちらもご覧ください

GetEnvironmentVariables(EnvironmentVariableTarget)

現在のプロセスから、または Windows オペレーティング システムのレジストリ キー (現在のユーザー用に予約されているレジストリ キーまたはローカル コンピューター用に予約されているレジストリ キー) から、すべての環境変数の名前と値を取得します。Retrieves all environment variable names and their values from the current process, or from the Windows operating system registry key for the current user or local machine.

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 つ。One of the EnvironmentVariableTarget values. Unix ベースのシステムで実行される .NET Core では、Process のみがサポートされています。Only Process is supported on .NET Core running on Unix-based systems.

戻り値

target パラメーターに指定したソースから取得された、すべての環境変数の名前と値を保持するディクショナリ。環境変数が見つからない場合は空のディクショナリ。A dictionary that contains all environment variable names and their values from the source specified by the target parameter; otherwise, an empty dictionary if no environment variables are found.

例外

呼び出し元には、target の指定された値にこの操作を実行するために必要なアクセス許可がありません。The caller does not have the required permission to perform this operation for the specified value of target.

target に正しくない値が含まれています。target contains an illegal value.

次の例では、EnvironmentVariableTarget.ProcessEnvironmentVariableTarget.User、および @no__t 2 のターゲットの環境変数を作成し、オペレーティングシステムのレジストリにユーザーとコンピューターの環境変数が含まれているかどうかを確認してから、環境変数を削除します。The following example creates environment variables for the EnvironmentVariableTarget.Process, EnvironmentVariableTarget.User, and Machine targets, checks whether the operating system registry contains the user and machine environment variables, then deletes the environment variables. Unix ベースのシステム上の .NET はユーザー単位およびコンピューター単位の環境変数をサポートしていないため、EnvironmentVariableTarget.Process の値を持つ SetEnvironmentVariable(String, String)SetEnvironmentVariable(String, String, EnvironmentVariableTarget) のみが正常にプロセス環境ブロックに環境変数を格納します。Because .NET on Unix-based systems does not support per-user and per-machine environment variables, only SetEnvironmentVariable(String, String) and SetEnvironmentVariable(String, String, EnvironmentVariableTarget) with a value of EnvironmentVariableTarget.Process successfully store an environment variable to the process environment block.

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

注釈

環境変数の名前と値は、返された IDictionary オブジェクトにキーと値のペアとして格納されます。The names and values of the environment variables are stored as key/value pairs in the returned IDictionary object.

Windows システムの場合On Windows systems

Windows システムでは、target パラメーターは、ソースが現在のプロセスであるか、現在のユーザーのレジストリキーであるか、またはローカルコンピューターのレジストリキーであるかを指定します。On Windows systems, the target parameter specifies whether the source is the current process, the registry key for the current user, or the registry key for the local machine.

MacOS および Linux システムの場合On macOS and Linux systems

MacOS と Linux では、EnvironmentVariableTarget.Process の @no__t 0 値のみがサポートされています。On macOS and Linux, only a target value of EnvironmentVariableTarget.Process is supported. プロセスごとの環境変数は、@no__t 0 プロセスを起動するために使用される親プロセス (通常はシェル) から継承されます。または、@no__t 1 プロセス自体のスコープ内で定義されます。Per-process environment variables are inherited from the parent process (typically the shell) used to launch the dotnet process or are defined within the scope of the dotnet process itself. Dotnet プロセスが終了すると、これらの後者の環境変数は存在しなくなります。Once the dotnet process ends, these latter environment variables cease to exist.

コンピューターごとおよびユーザーごとの環境変数はサポートされていません。Per-machine and per-user environment variables are not supported. @No__t-1 または EnvironmentVariableTarget.User の @no__t 0 値は、空の配列を返します。A target value of EnvironmentVariableTarget.Machine or EnvironmentVariableTarget.User returns an empty array.

セキュリティ

EnvironmentPermission

target が @no__t の場合に環境変数の名前と値を読み取る機能 (関連付けられた列挙: Read)、for the ability to read the names and values of environment variables if target is Process (Associated enumeration: Read), oror targetUser または Machine (関連付けられている列挙型: Unrestricted) の場合、環境変数へのフルアクセス。for full access to environment variables if target is User or Machine (Associated enumeration: Unrestricted).

こちらもご覧ください

適用対象