Environment.SetEnvironmentVariable 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
建立、修改或刪除環境變數。
多載
SetEnvironmentVariable(String, String) |
建立、修改或刪除儲存在目前進程中的環境變數。 |
SetEnvironmentVariable(String, String, EnvironmentVariableTarget) |
建立、修改或刪除儲存在目前進程或保留給目前使用者或本機計算機的 Windows 作業系統登錄機碼中的環境變數。 |
SetEnvironmentVariable(String, String)
建立、修改或刪除儲存在目前進程中的環境變數。
public:
static void SetEnvironmentVariable(System::String ^ variable, System::String ^ value);
public static void SetEnvironmentVariable (string variable, string value);
public static void SetEnvironmentVariable (string variable, string? value);
static member SetEnvironmentVariable : string * string -> unit
Public Shared Sub SetEnvironmentVariable (variable As String, value As String)
參數
- variable
- String
環境變數的名稱。
- value
- String
要指派給 variable
的值。
例外狀況
variable
null
。
variable
包含長度為零的字串、初始十六進位零字元(0x00),或等號 (“=” )。
-或-
variable
或 value
長度大於或等於 32,767 個字元。
-或-
執行此作業期間發生錯誤。
呼叫端沒有執行這項作業的必要許可權。
範例
下列範例會嘗試從進程環境區塊擷取名為 Test1
的環境變數值。 如果變數不存在,此範例會建立變數並擷取其值。 此範例會顯示變數的值。 針對在 Windows 系統上執行的 .NET 實作,它也會使用 EnvironmentVariableTarget 列舉的每個成員呼叫 GetEnvironmentVariables(EnvironmentVariableTarget) 方法,以建立變數只能從目前的進程環境區塊擷取。 (類似 Unix 系統上的 .NET 實作僅支援進程環境區塊中的變數。最後,如果範例建立變數,則會將其刪除。
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.
備註
呼叫這個方法相當於呼叫具有 target
自變數 EnvironmentVariableTarget.Process 值的 SetEnvironmentVariable(String, String, EnvironmentVariableTarget) 多載。
在類似 Unix 的系統上,對 SetEnvironmentVariable(String, String) 方法的呼叫不會影響任何原生連結庫,或將會載入。 (相反地,由原生連結庫所進行的進程環境修改,不會由Managed呼叫端看到。
如果 value
自變數未 null
,且 variable
參數所命名的環境變數不存在,則會建立環境變數並指派 value
的內容。 如果存在,則會修改其值。 因為環境變數只定義於目前進程的環境區塊中,所以不會在進程結束之後保存。
如果 variable
包含非初始十六進位零字元,則零字元之前的字元會被視為環境變數名稱,並忽略所有後續字元。
如果 value
包含非初始十六進位零字元,則會將零字元指派給環境變數之前的字元,並忽略所有後續字元。
如果 value
null
或 .NET 9 之前的版本中是空的,且 variable
所命名的環境變數存在,則會刪除環境變數。 如果 variable
不存在,即使無法執行作業,也不會發生任何錯誤。 在下列任何情況下,value
視為空白:
- String.Empty。
- 它包含單一字元,其值為U+0000。
另請參閱
適用於
SetEnvironmentVariable(String, String, EnvironmentVariableTarget)
建立、修改或刪除儲存在目前進程或保留給目前使用者或本機計算機的 Windows 作業系統登錄機碼中的環境變數。
public:
static void SetEnvironmentVariable(System::String ^ variable, System::String ^ value, EnvironmentVariableTarget target);
public static void SetEnvironmentVariable (string variable, string? value, EnvironmentVariableTarget target);
public static void SetEnvironmentVariable (string variable, string value, EnvironmentVariableTarget target);
static member SetEnvironmentVariable : string * string * EnvironmentVariableTarget -> unit
Public Shared Sub SetEnvironmentVariable (variable As String, value As String, target As EnvironmentVariableTarget)
參數
- variable
- String
環境變數的名稱。
- value
- String
要指派給 variable
的值。
- target
- EnvironmentVariableTarget
其中一個列舉值,指定環境變數的位置。
例外狀況
variable
null
。
variable
包含長度為零的字串、初始十六進位零字元(0x00),或等號 (“=” )。
-或-
variable
長度大於或等於 32,767 個字元。
-或-
target
不是 EnvironmentVariableTarget 列舉的成員。
-或-
target
是 Machine 或 User,且 variable
長度大於或等於 255。
-或-
target
是 Process,且 value
長度大於或等於 32,767 個字元。
-或-
執行此作業期間發生錯誤。
呼叫端沒有執行這項作業的必要許可權。
範例
下列範例會建立 EnvironmentVariableTarget.Process、EnvironmentVariableTarget.User和 Machine 目標的環境變數,檢查操作系統登錄是否包含使用者和計算機環境變數,然後刪除環境變數。 由於類似 Unix 系統上的 .NET 不支援每個使用者和每部電腦的環境變數,因此只有 SetEnvironmentVariable(String, String) 和 SetEnvironmentVariable(String, String, EnvironmentVariableTarget),且值為 EnvironmentVariableTarget.Process 成功將環境變數儲存至進程環境區塊。
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...
備註
SetEnvironmentVariable(String, String, EnvironmentVariableTarget) 方法可讓您定義目前進程可用的環境變數(Process 值)。 目前進程環境區塊唯一的環境變數只會持續到進程結束為止。
此外,在僅限 Windows 系統上,SetEnvironmentVariable(String, String, EnvironmentVariableTarget) 方法可讓您定義環境變數,可供計算機上執行的所有進程使用(EnvironmentVariableTarget.Machine 值),以及使用者執行的所有進程(EnvironmentVariableTarget.User 值)。 每部計算機和每個用戶環境變數會複製到目前進程的環境區塊中。
在類似 Unix 的系統上,會忽略具有 EnvironmentVariableTarget.Machine 或 EnvironmentVariableTarget.User 值的 SetEnvironmentVariable(String, String, EnvironmentVariableTarget) 方法呼叫。
在類似 Unix 的系統上,呼叫值為 EnvironmentVariableTarget.Process 的 SetEnvironmentVariable(String, String, EnvironmentVariableTarget) 方法,不會影響任何原生連結庫,或將會載入。 (相反地,由原生連結庫所進行的進程環境修改,不會由Managed呼叫端看到。
如果 value
自變數未 null
,且 variable
自變數所命名的環境變數不存在,則會建立環境變數並指派 value
的內容。 如果存在,則會修改其值。
如果 variable
包含非初始十六進位零字元,則零字元之前的字元會被視為環境變數名稱,並忽略所有後續字元。
如果 value
包含非初始十六進位零字元,則會將零字元指派給環境變數之前的字元,並忽略所有後續字元。
如果 value
null
或 .NET 9 之前的版本中是空的,且 variable
所命名的環境變數存在,則會刪除環境變數。 在下列任何情況下,value
視為空白:
- String.Empty。
- 它包含單一字元,其值為U+0000。
如果 variable
不存在,雖然無法執行作業,但不會發生任何錯誤。 當 target
Machine時請小心,因為您不小心刪除會影響整個本機計算機的環境變數,而不只是目前的進程或使用者。
Windows 系統上的 EnvironmentVariableTarget.Machine 和 EnvironmentVariableTarget.User
如果 target
是 EnvironmentVariableTarget.User,則環境變數會儲存在本機計算機登錄的 HKEY_CURRENT_USER\Environment 機碼中。 它也會複製到以目前使用者身分執行的檔案總管實例。 然後,使用者從檔案總管啟動的任何新進程都會繼承環境變數。
同樣地,如果 target
是 EnvironmentVariableTarget.Machine,則環境變數會儲存在本機計算機登錄的 HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Control\Session Manager\Environment 機碼中。 它也會複製到檔案總管的所有實例。 環境變數接著會由從 [檔案總管] 啟動的任何新進程繼承。
如果 target
是 User 或 Machine,則 Windows WM_SETTINGCHANGE
訊息會通知設定作業的其他應用程式。
如果 target
是 EnvironmentVariableTarget.User 或 EnvironmentVariableTarget.Machine,建議長度 value
小於 2048 個字元。
另請參閱
- GetEnvironmentVariable(String, EnvironmentVariableTarget)
- GetEnvironmentVariables(EnvironmentVariableTarget)
- EnvironmentVariableTarget