C# program to get the OS version using Environment class

How to get the OS version in C# using Environment class:

Environment class is defined in System namespace. It has many important properties that gives different information of the current operating system.

To get the current operating system version using Environment class in C#, we can use the OSVersion property. Using this property, we can read the current platform identifier and version number of the platform.

In this post, we will learn how to use Environment.OSVersion with example.

Definition:

Environment.OSVersion is defined as below:

public static OperatingSystem OSVersion { get; }
  • It is a static property.
  • It returns an OperatingSystem object. We can read the values from this object. The platform identifier and version number are available in this object.

Exception thrown:

It throws one InvalidOperationException exception if it was unable to get the version information or the value received is not a member of PlatformID.

Example program:

Let’s take a look at the below program:

using System;

class HelloWorld {
  static void Main() {
    Console.WriteLine(System.Environment.OSVersion);
  }
}

It is printing the OSVersion. It will print one output something like below:

Unix 5.4.0.1042

You might also like