冰楓論壇

標題: [C#] 獲取作業系統環境參數 [打印本頁]

作者: whitefox    時間: 2023-12-13 10:59
標題: [C#] 獲取作業系統環境參數
這裡使用微軟內建的 Environment 類別來獲取相關環境參數
  1. //--------------獲取當前程序信息
  2. //當前程序啟動的命令,字符串
  3. Console.WriteLine("CommandLine:{0}", Environment.CommandLine);
  4. string[] arguments = Environment.GetCommandLineArgs();
  5. Console.WriteLine("GetCommandLineArgs:{0}", string.Join(", ", arguments));
  6. //獲取當前程序目錄
  7. Console.WriteLine("CurrentDirectory:{0}", Environment.CurrentDirectory);
  8. //獲取當前程序,工作的物理內存大小
  9. Console.WriteLine("WorkingSet: {0}", Environment.WorkingSet);
  10. //退出碼
  11. Console.WriteLine("ExitCode:{0}", Environment.ExitCode);
  12. //獲取當前程序是否開機啟動
  13. Console.WriteLine("HasShutdownStarted: {0}", Environment.HasShutdownStarted);
  14. //獲取當前追蹤信息
  15. Console.WriteLine("StackTrace: '{0}'", Environment.StackTrace);
  16. //獲取換行符
  17. Console.WriteLine("NewLine: {0}  first line{0}  second line{0}  third line", Environment.NewLine);

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

  25. //--------------獲取當前系統資訊
  26. //獲取當前計算機名稱
  27. Console.WriteLine("MachineName: {0}", Environment.MachineName);
  28. //獲取系統鏡像等版本
  29. Console.WriteLine("Version: {0}", Environment.Version.ToString());
  30. //獲取當前操作系統版本
  31. Console.WriteLine("OSVersion: {0}", Environment.OSVersion.ToString());
  32. //獲取當前系統目錄
  33. Console.WriteLine("GetFolderPath: {0}",
  34.                 Environment.GetFolderPath(Environment.SpecialFolder.System));
  35. //獲取當前計算機邏輯磁盤名稱列表
  36. String[] drives = Environment.GetLogicalDrives();
  37. Console.WriteLine("GetLogicalDrives: {0}", String.Join(", ", drives));
  38. //獲取系統目錄
  39. String str;
  40. String nl = Environment.NewLine;
  41. String query = "My system drive is %SystemDrive% and my system root is %SystemRoot%";
  42. str = Environment.ExpandEnvironmentVariables(query);
  43. Console.WriteLine("ExpandEnvironmentVariables: {0}  {1}", nl, str);
  44. //獲取系統臨時文件夾
  45. Console.WriteLine("GetEnvironmentVariable: {0}  My temporary directory is {1}.", nl,
  46.                         Environment.GetEnvironmentVariable("TEMP"));

  47. //--------------遍歷所有資訊
  48. Console.WriteLine("GetEnvironmentVariables: ");
  49. IDictionary environmentVariables = Environment.GetEnvironmentVariables();
  50. foreach (DictionaryEntry de in environmentVariables)
  51. {
  52.     Console.WriteLine("  {0} = {1}", de.Key, de.Value);
  53. }
複製代碼





歡迎光臨 冰楓論壇 (https://bingfong.com/) Powered by 冰楓