OperatingSystem.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Diagnostics;
  5. using System.Runtime.Serialization;
  6. namespace System
  7. {
  8. #if PROJECTN
  9. [Internal.Runtime.CompilerServices.RelocatedType("System.Runtime.Extensions")]
  10. #endif
  11. public sealed class OperatingSystem : ISerializable, ICloneable
  12. {
  13. private readonly Version _version;
  14. private readonly PlatformID _platform;
  15. private readonly string _servicePack;
  16. private string _versionString;
  17. public OperatingSystem(PlatformID platform, Version version) : this(platform, version, null)
  18. {
  19. }
  20. internal OperatingSystem(PlatformID platform, Version version, string servicePack)
  21. {
  22. if (platform < PlatformID.Win32S || platform > PlatformID.MacOSX)
  23. {
  24. throw new ArgumentOutOfRangeException(nameof(platform), platform, SR.Format(SR.Arg_EnumIllegalVal, platform));
  25. }
  26. if (version == null)
  27. {
  28. throw new ArgumentNullException(nameof(version));
  29. }
  30. _platform = platform;
  31. _version = version;
  32. _servicePack = servicePack;
  33. }
  34. public void GetObjectData(SerializationInfo info, StreamingContext context)
  35. {
  36. throw new PlatformNotSupportedException();
  37. }
  38. public PlatformID Platform => _platform;
  39. public string ServicePack => _servicePack ?? string.Empty;
  40. public Version Version => _version;
  41. public object Clone() => new OperatingSystem(_platform, _version, _servicePack);
  42. public override string ToString() => VersionString;
  43. public string VersionString
  44. {
  45. get
  46. {
  47. if (_versionString == null)
  48. {
  49. string os;
  50. switch (_platform)
  51. {
  52. case PlatformID.Win32S: os = "Microsoft Win32S "; break;
  53. case PlatformID.Win32Windows: os = (_version.Major > 4 || (_version.Major == 4 && _version.Minor > 0)) ? "Microsoft Windows 98 " : "Microsoft Windows 95 "; break;
  54. case PlatformID.Win32NT: os = "Microsoft Windows NT "; break;
  55. case PlatformID.WinCE: os = "Microsoft Windows CE "; break;
  56. case PlatformID.Unix: os = "Unix "; break;
  57. case PlatformID.Xbox: os = "Xbox "; break;
  58. case PlatformID.MacOSX: os = "Mac OS X "; break;
  59. default:
  60. Debug.Fail($"Unknown platform {_platform}");
  61. os = "<unknown> "; break;
  62. }
  63. _versionString = string.IsNullOrEmpty(_servicePack) ?
  64. os + _version.ToString() :
  65. os + _version.ToString(3) + " " + _servicePack;
  66. }
  67. return _versionString;
  68. }
  69. }
  70. }
  71. }