whitefox 發表於 2023-12-13 10:59:20

[C#] 獲取作業系統環境參數

這裡使用微軟內建的 Environment 類別來獲取相關環境參數//--------------獲取當前程序信息
//當前程序啟動的命令,字符串
Console.WriteLine("CommandLine:{0}", Environment.CommandLine);
string[] arguments = Environment.GetCommandLineArgs();
Console.WriteLine("GetCommandLineArgs:{0}", string.Join(", ", arguments));
//獲取當前程序目錄
Console.WriteLine("CurrentDirectory:{0}", Environment.CurrentDirectory);
//獲取當前程序,工作的物理內存大小
Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet);
//退出碼
Console.WriteLine("ExitCode:{0}", Environment.ExitCode);
//獲取當前程序是否開機啟動
Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted);
//獲取當前追蹤信息
Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);
//獲取換行符
Console.WriteLine("NewLine: {0}  first line{0}  second line{0}  third line", Environment.NewLine);

//--------------獲取當前用戶資訊
//當前用戶的域名稱
Console.WriteLine("UserDomainName: {0}", Environment.UserDomainName);
//當前用戶是否交互
Console.WriteLine("UserInteractive: {0}", Environment.UserInteractive);
//當前用戶名稱
Console.WriteLine("UserName: {0}", Environment.UserName);

//--------------獲取當前系統資訊
//獲取當前計算機名稱
Console.WriteLine("MachineName: {0}", Environment.MachineName);
//獲取系統鏡像等版本
Console.WriteLine("Version: {0}", Environment.Version.ToString());
//獲取當前操作系統版本
Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());
//獲取當前系統目錄
Console.WriteLine("GetFolderPath: {0}",
                Environment.GetFolderPath(Environment.SpecialFolder.System));
//獲取當前計算機邏輯磁盤名稱列表
String[] drives = Environment.GetLogicalDrives();
Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", drives));
//獲取系統目錄
String str;
String nl = Environment.NewLine;
String query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
str = Environment.ExpandEnvironmentVariables(query);
Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", nl, str);
//獲取系統臨時文件夾
Console.WriteLine("GetEnvironmentVariable: {0}  My temporary directory is {1}.", nl,
                        Environment.GetEnvironmentVariable("TEMP"));

//--------------遍歷所有資訊
Console.WriteLine("GetEnvironmentVariables: ");
IDictionary environmentVariables = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry de in environmentVariables)
{
    Console.WriteLine("  {0} = {1}", de.Key, de.Value);
}
頁: [1]
查看完整版本: [C#] 獲取作業系統環境參數