Reader Level:
Articles

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.
  • 0
  • 0
  • 39191

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());

            Console.WriteLine();

 

            // 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);

 

            Console.ReadLine();

        }

    }

}


The output will look like following:

OS.jpg




Mahesh Chand

Founder C# Corner, a member-contributions based community founded in 1999 focuses on character, values, learning, sharing, networking and career growth. Mahesh has been awarded prestigious... Read more

COMMENT USING