OSEnvironmentHelper.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //----------------------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //----------------------------------------------------------------------------
  4. namespace System.ServiceModel
  5. {
  6. using System.Runtime;
  7. static class OSEnvironmentHelper
  8. {
  9. private static readonly OSVersion currentVersion;
  10. private static readonly byte currentServicePack;
  11. static OSEnvironmentHelper()
  12. {
  13. currentServicePack = (byte)Environment.OSVersion.Version.MajorRevision;
  14. int major = Environment.OSVersion.Version.Major;
  15. int minor = Environment.OSVersion.Version.Minor;
  16. if ((major < 5) || ((major == 5) && (minor == 0)))
  17. {
  18. currentVersion = OSVersion.PreWinXP;
  19. }
  20. if ((major == 5) && (minor == 1))
  21. {
  22. currentVersion = OSVersion.WinXP;
  23. }
  24. else if ((major == 5) && (minor == 2))
  25. {
  26. currentVersion = OSVersion.Win2003;
  27. }
  28. else if ((major == 6) && (minor == 0))
  29. {
  30. currentVersion = OSVersion.WinVista;
  31. }
  32. else if ((major == 6) && (minor == 1))
  33. {
  34. currentVersion = OSVersion.Win7;
  35. }
  36. else if ((major == 6) && (minor == 2))
  37. {
  38. currentVersion = OSVersion.Win8;
  39. }
  40. else if ((major > 6) ||
  41. ((major == 6) && (minor > 2)))
  42. {
  43. currentVersion = OSVersion.PostWin8;
  44. }
  45. else
  46. {
  47. // This should only be possible if major == 5 and minor > 2
  48. Fx.Assert(false, "Unknown OS");
  49. currentVersion = OSVersion.Unknown;
  50. }
  51. }
  52. internal static bool IsVistaOrGreater
  53. {
  54. get
  55. {
  56. return IsAtLeast(OSVersion.WinVista);
  57. }
  58. }
  59. internal static bool IsApplicationTargeting45
  60. {
  61. get
  62. {
  63. #pragma warning disable 0618
  64. return System.Net.WebSockets.WebSocket.IsApplicationTargeting45();
  65. #pragma warning restore 0618
  66. }
  67. }
  68. internal static int ProcessorCount
  69. {
  70. get
  71. {
  72. return Environment.ProcessorCount;
  73. }
  74. }
  75. internal static bool IsAtLeast(OSVersion version)
  76. {
  77. return IsAtLeast(version, 0);
  78. }
  79. static bool IsAtLeast(OSVersion version, byte servicePack)
  80. {
  81. Fx.Assert(version != OSVersion.Unknown, "Unknown OS");
  82. if (servicePack == 0)
  83. {
  84. return version <= currentVersion;
  85. }
  86. // If a SP value is provided and we have the same OS version, compare SP values
  87. if (version == currentVersion)
  88. {
  89. return servicePack <= currentServicePack;
  90. }
  91. return version < currentVersion;
  92. }
  93. }
  94. }