You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Apewer
  5. {
  6. /// <summary>系统实用工具。</summary>
  7. public class SystemUtility
  8. {
  9. #if NETSTD || NETCORE
  10. /// <summary>当前操作系统是 Windows。</summary>
  11. public static bool IsWindows { get => System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows); }
  12. /// <summary>当前操作系统是 OS X 或 macOS。</summary>
  13. public static bool IsOSX { get => System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX); }
  14. /// <summary>当前操作系统是 Linux。</summary>
  15. public static bool IsLinux { get => System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux); }
  16. #endif
  17. /// <summary></summary>
  18. public static void SetConsoleCtrlCancel(Func<bool> exit)
  19. {
  20. const string postfix = " - 按 CTRL + C 可安全退出";
  21. var title = Console.Title;
  22. if (!title.EndsWith(postfix))
  23. {
  24. title = title + postfix;
  25. Console.Title = title;
  26. }
  27. if (exit == null) return;
  28. Console.CancelKeyPress += (s, e) =>
  29. {
  30. e.Cancel = !exit();
  31. };
  32. }
  33. }
  34. }