C# 取得環境變數 Environment

依照名稱取得使用者環境變數 "PATH" 的內容:
    
string? path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.User);
    

依照名稱取得系統環境變數 "PATH" 的內容:
    
string? path = Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine);
    

列出所有環境變數:
    
using System.Collections;

foreach (DictionaryEntry dictionaryEntry in Environment.GetEnvironmentVariables())
{
    Console.WriteLine($"{dictionaryEntry.Key} = {dictionaryEntry.Value}");
}
    



參考資料:
Microsoft.Learn - Environment Class
Microsoft.Learn -Environment.GetEnvironmentVariable Method

留言