Go to content Go to navigation Go to search

Brokenwire.NET::Programming

Get the OS version in C#
· Apr 2, 12:42 PM by Thijs Kroesbergen

After I wrote about the new wave of operating systems written in C# I noticed an increase in traffic from people who are looking for something completely different.

They did type C#, Operating system and Version into their favorite search engine, and they found this site. But they didn't find an answer to their question.

So for all these people I now show you how to determine the OS version using C# (and the .NET framework).

The very short answer:

System.OperatingSystem osInfo
=
System.Environment.OSVersion;

By looking at osInfo.Version property and the table with version numbers on the Windows Wikipedia page you can determine the OS version.

Or with PowerShell (just because it's possible)

[System.Environment]::OsVersion

Now on to the more sophisticated version. If you want not only the major versions but also the details about the product types and even information about the edition (suite) the machine is running, then you can use some (more complicated) API's, like GetVersionEx.

First of all you need to understand how the OS version number scheme of Windows works. There is a great article about this subject written by Marius Bancila. In this article he explains how the version scheme is laid out. He also shows how to use C++ and the Platform SDK to get to the information we need.

But you wanted to use C# (or .NET in general), so I've also dug up an article on CodeProject which shows the C# code to get the operating system version. The article on CodeProject is not up-to-date (think Vista, server 2008) but by combining the two articles you can create the complete OS detection code. I leave this as an exercise for those of you who really need this...

Have fun!

- Permalink -

Name
E-mail
http://
Message
  Textile Help