SystemVersionInfo.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // reference from https://github.com/sourcechord/FluentWPF/blob/master/FluentWPF/Utility/VersionInfo.cs
  2. // LICENSE: https://github.com/sourcechord/FluentWPF/blob/master/LICENSE
  3. using System;
  4. namespace HandyControl.Data;
  5. public readonly struct SystemVersionInfo
  6. {
  7. public static SystemVersionInfo Windows10 => new(10, 0, 10240);
  8. public static SystemVersionInfo Windows10_1809 => new(10, 0, 17763);
  9. public static SystemVersionInfo Windows10_1903 => new(10, 0, 18362);
  10. public SystemVersionInfo(int major, int minor, int build)
  11. {
  12. Major = major;
  13. Minor = minor;
  14. Build = build;
  15. }
  16. public int Major { get; }
  17. public int Minor { get; }
  18. public int Build { get; }
  19. public bool Equals(SystemVersionInfo other) => Major == other.Major && Minor == other.Minor && Build == other.Build;
  20. public override bool Equals(object obj) => obj is SystemVersionInfo other && Equals(other);
  21. public override int GetHashCode() => Major.GetHashCode() ^ Minor.GetHashCode() ^ Build.GetHashCode();
  22. public static bool operator ==(SystemVersionInfo left, SystemVersionInfo right) => left.Equals(right);
  23. public static bool operator !=(SystemVersionInfo left, SystemVersionInfo right) => !(left == right);
  24. public int CompareTo(SystemVersionInfo other)
  25. {
  26. if (Major != other.Major)
  27. {
  28. return Major.CompareTo(other.Major);
  29. }
  30. if (Minor != other.Minor)
  31. {
  32. return Minor.CompareTo(other.Minor);
  33. }
  34. if (Build != other.Build)
  35. {
  36. return Build.CompareTo(other.Build);
  37. }
  38. return 0;
  39. }
  40. public int CompareTo(object obj)
  41. {
  42. if (!(obj is SystemVersionInfo other))
  43. {
  44. throw new ArgumentException();
  45. }
  46. return CompareTo(other);
  47. }
  48. public static bool operator <(SystemVersionInfo left, SystemVersionInfo right) => left.CompareTo(right) < 0;
  49. public static bool operator <=(SystemVersionInfo left, SystemVersionInfo right) => left.CompareTo(right) <= 0;
  50. public static bool operator >(SystemVersionInfo left, SystemVersionInfo right) => left.CompareTo(right) > 0;
  51. public static bool operator >=(SystemVersionInfo left, SystemVersionInfo right) => left.CompareTo(right) >= 0;
  52. public override string ToString() => $"{Major}.{Minor}.{Build}";
  53. }