Loading...
February 11, 2008#

.NET Programming: OS and Service Pack Version

Yet another short article about how to programmatically detect the version of the operating system and installed service pack version in a .NET application (C#).

Basically I found three ways how to determine these version number in a C# application:

  1. Using the System.Environment.OSVersion class
    - Fast and easy, perfect to get Windows version including major, minor, revision and build section of the version number.
    - Problematic to get the service pack version: Gives you a string like e.g. ‘Service Pack 1, v.744′ which you’d have to parse then. Not an ideal and very stable way (regarding future service packs) to get the service pack version number.
  2. Using the WMI Interface
    SelectQuery query = new SelectQuery("Win32_OperatingSystem");
    ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
    foreach (ManagementObject mo in searcher.Get())
    int iSPVersionMajor = int.Parse(mo["ServicePackMajorVersion"].ToString());

    Allows to retrieve numeric (major and minor) version number of installed service pack

  3. Using native Win32 API: GetVersionEx

    I didn’t try this solution myself but it seems quite promising, too. More details can be found here.

Last but not least, some information about Windows Version and Build numbers: