Find an Operating System version

How Do I detect an OS version? 

In Win32 SDK, we have an API GetVersionEx that returns the information in OSVERSIONINFO structure. And then we can look at the values of various members of structures to decide what version of operating system we are running on.

To accomplish the same task, .NET SDK provides a class named Environment. In this class there is a static method named OSVersion that returns OperatingSystem object. This object can be used to get the value of operating system version. 

How Do I Interpret OperatingSystem Object

The OperatingSystem class has three properties that contain all the information that you will need to get value of operating system version. 

  1. Platform: This property returns a PlatformID value. This enum has three possible values.

    • Win32NT Operating system is Windows NT
    • Win32Windows Operating system is Windows 95 or later
    • Win32S Operating system is Win32s running on a 16-bit version of Windows.

  2. CSD: This property indicates Corrected Service Diskette number of the operating system or in other words this is a string representing the service pack installed for the operating system.

  3. Version: This property returns a Version class object. This class is nothing but the standard version class used for indicating any assembly's version. .NET defines a version value in the format Major.Minor.Revision.Build. The Version class has four properties that completely define the version of operating system or an assembly.
    • Major - Major Version Number
    • Minor - Minor Version Number
    • Revision - Revision Number
    • Build - Build Number

I would suggest you read documentation to get in depth details about what these values stand for and how .NET uses these values to load the right version of an assembly for any application.  

What All These Values Mean

You can combine the values returned by the above three values to get the exact version of OS (Windows) running on the system. Since there are no details available in .NET SDK, I assume the meaning of all the values remain the same as Win32 SDK. Following is the table I used to extract the value of Operating System.

PlatformIDMajor VersionMinor VersionOperating System
Win32Windows>= 40Win95
Win32Windows>= 4> 0 && < 90Win98
Win32Windows>= 4> 0 && >= 90WinMe
Win32NT<= 40WinNT
Win32NT 50Win2K
Win32NT 5> 0WinXP

Following a small sample from the class that shows the use of Environment class in System namespace. 

Private
 os As OperatingSystem = Environment.OSVersion
' Get the version information
Private vs As Version = os.Version
Private Me.m_nMajorVer = vs.Major
Private Me.m_nMinorVersion = vs.Minor
Private Me.m_nRevision = vs.Revision
Private Me
.m_nBuildNumber = vs.Build
' Get the service pack information
Private Me.m_strServicePack = os.CSD


Similar Articles