Reader Level:

Get Operating System Data and Version in C#

By Mahesh Chand on Feb 29, 2008
In one of my applications, I needed Operating System version, service pack and other details. Here is the code snippet that shows how to use the Environment class to get Operating System data.

The following code snippet displays your Operating System's version, SP, and platform details by using the OperatingSystem object and its members.

The Version class and its properties Major, Minor, and Build represent the major, minor, and build numbers.

using System;


namespace OsVersionSample


    class Program


        static void Main(string[] args)


            Console.WriteLine("Operating System Detaiils");

            OperatingSystem os = Environment.OSVersion;

            Console.WriteLine("OS Version: " + os.Version.ToString());

            Console.WriteLine("OS Platoform: " + os.Platform.ToString());

            Console.WriteLine("OS SP: " + os.ServicePack.ToString());

            Console.WriteLine("OS Version String: " + os.VersionString.ToString());



            // Get Version details

            Version ver = os.Version;

            Console.WriteLine("Major version: " + ver.Major);

            Console.WriteLine("Major Revision: " + ver.MajorRevision);

            Console.WriteLine("Minor version: " + ver.Minor);

            Console.WriteLine("Minor Revision: " + ver.MinorRevision);

            Console.WriteLine("Build: " + ver.Build);






The output will look like following:


Mahesh Chand
Mahesh Chand

C# Corner founder, 10-times Microsoft MVP, technical author, and software architect. Mahesh authored his first book, A Programmer’s Guide to ADO.NET in C# with APress at the age of 25. C# Corner is a memb... Read more



JetBrains ReSharper Ultimate
.NET code analysis, refactorings, navigation, unit testing, code coverage and profiling in Visual Studio, a bonus for C++ devs, bundled at 50% off.

Trending up