Environment.GetEnvironmentVariable Environment.GetEnvironmentVariable Environment.GetEnvironmentVariable Environment.GetEnvironmentVariable Method

定义

检索环境变量的值。Retrieves the value of an environment variable.

重载

GetEnvironmentVariable(String) GetEnvironmentVariable(String) GetEnvironmentVariable(String) GetEnvironmentVariable(String)

从当前进程检索环境变量的值。Retrieves the value of an environment variable from the current process.

GetEnvironmentVariable(String, EnvironmentVariableTarget) GetEnvironmentVariable(String, EnvironmentVariableTarget) GetEnvironmentVariable(String, EnvironmentVariableTarget) GetEnvironmentVariable(String, EnvironmentVariableTarget)

从当前进程或者从当前用户或本地计算机的 Windows 操作系统注册表项检索环境变量的值。Retrieves the value of an environment variable from the current process or from the Windows operating system registry key for the current user or local machine.

GetEnvironmentVariable(String) GetEnvironmentVariable(String) GetEnvironmentVariable(String) GetEnvironmentVariable(String)

从当前进程检索环境变量的值。Retrieves the value of an environment variable from the current process.

public:
 static System::String ^ GetEnvironmentVariable(System::String ^ variable);
public static string GetEnvironmentVariable (string variable);
static member GetEnvironmentVariable : string -> string
Public Shared Function GetEnvironmentVariable (variable As String) As String

参数

variable
String String String String

环境变量名。The name of the environment variable.

返回

variable 指定的环境变量的值;或者如果找不到环境变量,则返回 nullThe value of the environment variable specified by variable, or null if the environment variable is not found.

异常

调用方没有执行此操作所需的权限。The caller does not have the required permission to perform this operation.

示例

下面的示例使用GetEnvironmentVariable方法来windir检索包含 Windows 目录路径的环境变量。The following example uses the GetEnvironmentVariable method to retrieve the windir environment variable, which contains the path of the Windows directory.

using namespace System;
using namespace System::IO;

void main()
{
      if (Environment::OSVersion->Platform == PlatformID::Win32NT)
      {
            // Change the directory to %WINDIR%
            Environment::CurrentDirectory = Environment::GetEnvironmentVariable( "windir" );
            DirectoryInfo^ info = gcnew DirectoryInfo( "." );

            Console::WriteLine("Directory Info:   {0}", 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.
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.
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.

下面的示例尝试从进程环境块中检索名为Test1的环境变量的值。The following example attempts to retrieve the value of an environment variable named Test1 from the process environment block. 如果该变量不存在,则该示例将创建其并检索其值。If the variable doesn't exist, the example creates its and retrieves its value. 该示例显示变量的值。The example displays the value of the variable. 如果该示例创建了变量,它还将对GetEnvironmentVariables(EnvironmentVariableTarget) EnvironmentVariableTarget枚举的每个成员调用方法,以确定只能从当前进程环境块检索变量。If the example created the variable, it also calls the GetEnvironmentVariables(EnvironmentVariableTarget) method with each member of the EnvironmentVariableTarget enumeration to establish that the variable can be retrieved only from the current process environment block. 最后,如果该示例创建了变量,则将其删除。Finally, if the example created the variable, it deletes it.

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

注解

GetEnvironmentVariable(String)方法只从当前进程的环境块中检索环境变量。The GetEnvironmentVariable(String) method retrieves an environment variable from the environment block of the current process only. 它等效于GetEnvironmentVariable(String, EnvironmentVariableTarget) target使用值EnvironmentVariableTarget.Process调用方法。It is equivalent to calling the GetEnvironmentVariable(String, EnvironmentVariableTarget) method with a target value of EnvironmentVariableTarget.Process.

若要检索所有环境变量及其值,请调用GetEnvironmentVariables方法。To retrieve all environment variables along with their values, call the GetEnvironmentVariables method.

环境变量名称在 Linux 和 macOS 上区分大小写,但在 Windows 上不区分大小写。Environment variable names are case-sensitive on Linux and macOS but are not case-sensitive on Windows.

在 Windows 系统上On Windows systems

在 Windows 系统中,当前进程的环境块包括:On Windows systems, the environment block of the current process includes:

  • 由创建它的父进程为其提供的所有环境变量。All environment variables that are provided to it by the parent process that created it. 例如,从控制台窗口启动的 .NET 应用程序继承了控制台窗口的所有环境变量。For example, a .NET application launched from a console window inherits all of the console window's environment variables.

    如果没有父进程,则改用每个计算机和每个用户的环境变量。If there is no parent process, per-machine and per-user environment variables are used instead. 例如,新的控制台窗口包含启动时定义的每个计算机和每个用户的环境变量。For example, a new console window has all per-machine and per-user environment variables defined at the time it was launched.

  • 当进程正在运行时, SetEnvironmentVariable(String, String)通过使用target值调用方法SetEnvironmentVariable(String, String, EnvironmentVariableTarget)EnvironmentVariableTarget.Process方法,添加到进程块的任何变量。Any variables added to the process block 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. 这些环境变量将一直保持到 .NET 应用程序终止为止。These environment variables persist until the .NET application terminates.

如果在进程启动之后创建环境变量,则可以使用此方法只检索通过SetEnvironmentVariable(String, String)调用方法SetEnvironmentVariable(String, String, EnvironmentVariableTarget)EnvironmentVariableTarget.Process使用target值的方法创建的那些变量。If environment variables are created after the process has started, you can use this method to retrieve only those variables that were created by calling 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 上,当前进程的环境块包含以下环境变量:On macOS and Linux, the environment block of the current process includes the following environment variables:

MacOS 和 Linux 上的 .NET Core 不支持每计算机或每用户环境变量。.NET Core on macOS and Linux does not support per-machine or per-user environment variables.

安全性

EnvironmentPermission
用于读取的值variablefor the ability to read the value of variable. 关联的枚举:ReadAssociated enumeration: Read

另请参阅

GetEnvironmentVariable(String, EnvironmentVariableTarget) GetEnvironmentVariable(String, EnvironmentVariableTarget) GetEnvironmentVariable(String, EnvironmentVariableTarget) GetEnvironmentVariable(String, EnvironmentVariableTarget)

从当前进程或者从当前用户或本地计算机的 Windows 操作系统注册表项检索环境变量的值。Retrieves the value of an environment variable from the current process or from the Windows operating system registry key for the current user or local machine.

public:
 static System::String ^ GetEnvironmentVariable(System::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

参数

variable
String String String String

环境变量名。The name of an environment variable.

target
EnvironmentVariableTarget EnvironmentVariableTarget EnvironmentVariableTarget EnvironmentVariableTarget

EnvironmentVariableTarget 值之一。One of the EnvironmentVariableTarget values. 在基于 Unix 的系统上运行的 .NET Core 仅支持 ProcessOnly Process is supported on .NET Core running on Unix-bases systems.

返回

variabletarget 参数指定的环境变量的值;或者如果找不到环境变量,则返回 nullThe value of the environment variable specified by the variable and target parameters, or null if the environment variable is not found.

异常

调用方没有执行此操作所需的权限。The caller does not have the required permission to perform this operation.

示例

下面的示例为EnvironmentVariableTarget.ProcessEnvironmentVariableTarget.UserMachine目标创建环境变量,检查操作系统注册表是否包含用户和计算机环境变量,然后删除环境变化.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 不支持每用户和每台计算机的环境变量,因此SetEnvironmentVariable(String, String)只有SetEnvironmentVariable(String, String, EnvironmentVariableTarget)和值EnvironmentVariableTarget.Process才能成功地将环境变量存储到进程环境块。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...

注解

若要检索所有环境变量及其值,请调用GetEnvironmentVariables方法。To retrieve all environment variables along with their values, call the GetEnvironmentVariables method.

环境变量名称在 Linux 和 macOS 上区分大小写,但在 Windows 上不区分大小写。Environment variable names are case-sensitive on Linux and macOS but are not case-sensitive on Windows.

在 Windows 系统上On Windows systems

在 Windows 上, target参数指定是从当前进程还是从当前用户或本地计算机的 Windows 操作系统注册表项检索环境变量。On Windows, the target parameter specifies whether the environment variable is retrieved from the current process or from the Windows operating system registry key for the current user or local machine. 所有基于用户和每台计算机的环境变量都将自动复制到当前进程的环境块中,这与创建 .NET 进程的父进程可用的任何其他环境变量一样。All per-user and per-machine environment variables are automatically copied into the environment block of the current process, as are any other environment variables that are available to the parent process that created the .NET process. 但是,通过SetEnvironmentVariable(String, String)在进程的持续时间内调用方法SetEnvironmentVariable(String, String, EnvironmentVariableTarget)EnvironmentVariableTarget.Process方法( target其值仅为持久性),只会将环境变量添加到当前进程的环境块。However, environment variables added only to the environment block of the current process by calling either the SetEnvironmentVariable(String, String) method or the SetEnvironmentVariable(String, String, EnvironmentVariableTarget) method with a target value of EnvironmentVariableTarget.Process persist only for the duration of the process.

在 macOS 和 Linux 系统上On macOS and Linux systems

在 macOS 和 Linux 上, GetEnvironmentVariable(String, EnvironmentVariableTarget)方法EnvironmentVariableTarget.Processtarget支持值。On macOS and Linux, the GetEnvironmentVariable(String, EnvironmentVariableTarget) method supports a target value of EnvironmentVariableTarget.Process only. 不支持EnvironmentVariableTarget.Machine EnvironmentVariableTarget.User值为或的调用,返回nulltargetCalls with a target value of EnvironmentVariableTarget.Machine or EnvironmentVariableTarget.User are not supported and return null.

每进程环境变量为:Per-process environment variables are:

安全性

EnvironmentPermission
variable如果Process Read是(关联的枚举:),则可以读取的值。 targetfor the ability to read the value of variable if target is Process (Associated enumeration: Read), oror 如果target为或User Unrestricted(关联的枚举:),则表示完全访问环境变量。 Machinefor full access to environment variables if target is User or Machine (Associated enumeration: Unrestricted).

另请参阅

适用于