c sharp

C# Environment.OSVersion Property

You are writing and running so many C# programs. What do you think your operating system version is in which you are running these applications?

Let’s check our Operating System version by reading this article.

Environment.OSVersion

This property is available in Environment Class. C# provides information about the working environment like variables, methods used, and system related information.

OSVersion returns the Operating system with platform identifier and version number.

Syntax:

string Environment.MachineName

 
This property can be applied in .NET 2.0,2.1,3.0.3.1,3,5,5,6 and 7.

It returns the InvalidOperationException when the OSVersion property does not obtain the version.

Example 1:

Let’s return the operating system version that we are running our C# Program.


 
Explanation:

Inside the Main method:

Line 6:

Console.WriteLine("The operating System Version that you are using: "+Environment.OSVersion);

 
We are directly getting the Operating System version using OSVersion Property.

So, the version that returned is Unix 5.4.0.1080

It can be possible to declare a variable and store that version and print it.

We have to store the return property in a variable defined by a datatype called System.OperatingSystem. It stores the operating system type.

Syntax:

System.OperatingSystem variable_name=Environment.OSVersion;

 

Example 2:

Let’s demonstrate the above scenario.


Here, we set the OSVersion property to the variable – version_var that stores the OSVersion. Finally, we can get the version from the variable.

Conclusion

In this C# Tutorial, we discussed how to get the operating system version using OSVersion property with two examples. OSVersion returns the Operating system with platform identifier and version number. If the OSVersion property does not obtain the version, then it returns the exception – InvalidOperationException.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain