1 // ==++==
2 //
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 //
5 // ==--==
6 /*============================================================
7 **
8 ** Class:    OperatingSystem
9 **
10 **
11 ** Purpose:
12 **
13 **
14 ===========================================================*/
15 namespace System {
16     using System.Runtime.Serialization;
17     using System.Globalization;
18     using System.Security.Permissions;
19     using System.Runtime.InteropServices;
20     using System.Diagnostics.Contracts;
21 
22 
23     [ComVisible(true)]
24     [Serializable]
25     public sealed class OperatingSystem : ICloneable , ISerializable
26     {
27         private Version _version;
28         private PlatformID _platform;
29         private string _servicePack;
30         private string _versionString;
31 
OperatingSystem()32         private OperatingSystem()
33         {
34         }
35 
OperatingSystem(PlatformID platform, Version version)36         public OperatingSystem(PlatformID platform, Version version) : this(platform, version, null) {
37         }
38 
OperatingSystem(PlatformID platform, Version version, string servicePack)39         internal OperatingSystem(PlatformID platform, Version version, string servicePack) {
40 #if !FEATURE_LEGACYNETCF
41             if( platform < PlatformID.Win32S || platform > PlatformID.MacOSX) {
42 #else // FEATURE_LEGACYNETCF
43             if( platform < PlatformID.Win32S || platform > PlatformID.NokiaS60) {
44 #endif // FEATURE_LEGACYNETCF
45                 throw new ArgumentException(
46                     Environment.GetResourceString("Arg_EnumIllegalVal", (int)platform),
47                     "platform");
48             }
49 
50             if ((Object) version == null)
51                 throw new ArgumentNullException("version");
52             Contract.EndContractBlock();
53 
54             _platform = platform;
55             _version = (Version) version.Clone();
56             _servicePack = servicePack;
57         }
58 
59         private OperatingSystem(SerializationInfo info, StreamingContext context) {
60             SerializationInfoEnumerator enumerator = info.GetEnumerator();
61             while( enumerator.MoveNext()) {
62                 switch( enumerator.Name) {
63                     case "_version":
64                         _version = (Version) info.GetValue("_version", typeof(Version));
65                         break;
66                     case "_platform":
67                         _platform = (PlatformID) info.GetValue("_platform", typeof(PlatformID));
68                         break;
69                     case "_servicePack":
70                         _servicePack = info.GetString("_servicePack");
71                         break;
72                 }
73             }
74 
75             if (_version == null ) {
76                 throw new SerializationException(Environment.GetResourceString("Serialization_MissField", "_version"));
77             }
78         }
79 
80         [System.Security.SecurityCritical]  // auto-generated_required
81         public void GetObjectData(SerializationInfo info, StreamingContext context) {
82             if( info == null ) {
83                 throw new ArgumentNullException("info");
84             }
85             Contract.EndContractBlock();
86 
87             info.AddValue("_version", _version);
88             info.AddValue("_platform", _platform);
89             info.AddValue("_servicePack", _servicePack);
90         }
91 
92         public PlatformID Platform {
93             get { return _platform; }
94         }
95 
96         public string ServicePack {
97             get {
98                 if( _servicePack == null) {
99                     return string.Empty;
100                 }
101 
102                 return _servicePack;
103             }
104         }
105 
106         public Version Version {
107             get { return _version; }
108         }
109 
110         public Object Clone() {
111             return new OperatingSystem(_platform,
112                                        _version, _servicePack );
113         }
114 
115         public override String ToString() {
116             return VersionString;
117         }
118 
119         public String VersionString {
120             get {
121                 if(_versionString != null) {
122                     return _versionString;
123                 }
124 
125                 String os;
126                 switch(_platform)
127                 {
128                     case PlatformID.Win32NT:
129                         os = "Microsoft Windows NT ";
130                         break;
131                     case PlatformID.Win32Windows:
132                         if ((_version.Major > 4) ||
133                             ((_version.Major == 4) && (_version.Minor > 0)))
134                             os = "Microsoft Windows 98 ";
135                         else
136                             os = "Microsoft Windows 95 ";
137                         break;
138                     case PlatformID.Win32S:
139                         os = "Microsoft Win32S ";
140                         break;
141                     case PlatformID.WinCE:
142                         os = "Microsoft Windows CE ";
143                         break;
144 #if !FEATURE_LEGACYNETCF
145                     case PlatformID.MacOSX:
146                         os = "Mac OS X ";
147                         break;
148 #endif
149                     default:
150                         os = "<unknown> ";
151                         break;
152                 }
153 
154                 if( String.IsNullOrEmpty(_servicePack)) {
155                     _versionString = os + _version.ToString();
156                 }
157                 else {
158                     _versionString = os + _version.ToString(3) + " " + _servicePack;
159                 }
160 
161                 return _versionString;
162             }
163         }
164     }
165 }
166