Go to content Go to navigation Go to search

Brokenwire.NET::Programming

Get the OS version in C#
· 2008-04-02 11:42 by Thijs Kroesbergen for Brokenwire.NET

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 -

  1. So kind, thanks :)
    But I have a problem:
    The PlatfomID enum contains only these platforms:

    Unix, Win32NT, Win32s, Win32Windows, WinCE

    There is no OSX! And now Mono runs on OSX too. (Maybe Microsoft would have never tought that .NET will run on OSX :D )


    — Lord Kitten    2009-08-25 10:19    #
  2. Mono’s System.PlatformId does have an entry for OSX. See the mono documentation here: http://www.go-mono.com/docs/index.aspx?link=T%3ASystem.PlatformID


    thijs    2009-08-25 11:54    #
  3. very thoughtful of you, I stumbled here through google looking for this. Thanks for including it


    Ben    2009-09-24 09:40    #
  4. From experience I can tell you that mono on OSX and Linux osInfo.Platform reports “Unix” on both (on others as well I beleive, but don’t know from experience.

    osInfo.VersionString, osInfo.Version

    Both report 10.4.0.0 (the Darwin Kernel Version) on OSX, On linux they report the linux kernel version. Hope that helps.


    Dave    2010-10-12 12:26    #