summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/OperatingSystem.cs
blob: a11d4bcce86342b0ca74fefa4deda48be137e332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*============================================================
**
**
**
** Purpose: 
**
**
===========================================================*/
namespace System {
    using System.Runtime.Serialization;
    using System.Globalization;
    using System.Security.Permissions;
    using System.Runtime.InteropServices;
    using System.Diagnostics.Contracts;


    [ComVisible(true)]
    [Serializable]
    public sealed class OperatingSystem : ICloneable , ISerializable
    {
        private Version _version;
        private PlatformID _platform;
        private string _servicePack;
        private string _versionString;

        private OperatingSystem()
        {
        }

        public OperatingSystem(PlatformID platform, Version version) : this(platform, version, null) {
        }
    
        internal OperatingSystem(PlatformID platform, Version version, string servicePack) {
            if( platform < PlatformID.Win32S || platform > PlatformID.MacOSX) {
                throw new ArgumentException(
                    Environment.GetResourceString("Arg_EnumIllegalVal", (int)platform),
                    nameof(platform));
            }

            if ((Object) version == null)
                throw new ArgumentNullException(nameof(version));
            Contract.EndContractBlock();

            _platform = platform;
            _version = (Version) version.Clone();
            _servicePack = servicePack;
        }
        
        private OperatingSystem(SerializationInfo info, StreamingContext context) {            
            SerializationInfoEnumerator enumerator = info.GetEnumerator();                        
            while( enumerator.MoveNext()) {
                switch( enumerator.Name) {
                    case "_version":
                        _version = (Version) info.GetValue("_version", typeof(Version));
                        break;
                    case "_platform":
                        _platform = (PlatformID) info.GetValue("_platform", typeof(PlatformID));
                        break;
                    case "_servicePack":
                        _servicePack = info.GetString("_servicePack");
                        break;
                }
            }

            if (_version == null ) {
                throw new SerializationException(Environment.GetResourceString("Serialization_MissField", "_version"));
            }
        }

        public void GetObjectData(SerializationInfo info, StreamingContext context) {
            if( info == null ) {
                throw new ArgumentNullException(nameof(info));
            }
            Contract.EndContractBlock();

            info.AddValue("_version", _version);
            info.AddValue("_platform", _platform);
            info.AddValue("_servicePack", _servicePack);
        }        

        public PlatformID Platform {
            get { return _platform; }
        }
        
        public string ServicePack { 
            get { 
                if( _servicePack == null) {
                    return string.Empty;
                }

                return _servicePack;
            }
        }    

        public Version Version {
            get { return _version; }
        }
    
        public Object Clone() {
            return new OperatingSystem(_platform,
                                       _version, _servicePack );
        }
    
        public override String ToString() {
            return VersionString;
        }

        public String VersionString {
            get {
                if(_versionString != null) {
                    return _versionString;
                }

                String os;
                switch(_platform)
                {
                    case PlatformID.Win32NT:
                        os = "Microsoft Windows NT ";
                        break;
                    case PlatformID.Win32Windows:
                        if ((_version.Major > 4) ||
                            ((_version.Major == 4) && (_version.Minor > 0)))
                            os = "Microsoft Windows 98 ";
                        else
                            os = "Microsoft Windows 95 ";
                        break;
                    case PlatformID.Win32S:
                        os = "Microsoft Win32S ";
                        break;
                    case PlatformID.WinCE:
                        os = "Microsoft Windows CE ";
                        break;
                    case PlatformID.MacOSX:
                        os = "Mac OS X ";
                        break;
                    default:
                        os = "<unknown> ";
                        break;
                }

                if( String.IsNullOrEmpty(_servicePack)) {
                    _versionString = os + _version.ToString();
                }
                else {
                    _versionString = os + _version.ToString(3) + " " + _servicePack;
                }

                return _versionString;            
            }
        }
    }
}