C detecting windows version

C# Detect Windows OS Version – Part 1

After reading this, check out Part 2: Using WMI for even more info

Introduction

I recently needed to programmatically determine the Operating System my software is running on. I was amazed at how difficult it was to find reliable, useful information on this subject. There were bits and pieces here and there, but almost everyone assumed I had some pre-existing knowledge of the process in one manner or another. Hopefully, this little series will save somebody some time in the future.

The thing that makes this so difficult is that there are almost as many different ways to detect the operating system as there are operating systems! It’s an unbelievably frustrating experience trying to reinvent this wheel, so I’ll try to walk through it here.

Strategies

There are 4 basic strategies:

  1. Read and parse information from files in the Windows directory. This is probably the most difficult and least rewarding method. The files are different for each generation of OS and the format is not guaranteed in almost any case.
  2. Read information from the registry. This method works a little better, but you still have varying locations for the information based on which OS you’re dealing with.
  3. Use Environment variables. Nothing is more reliable. However, it doesn’t give you the nitty-gritty detail you might want. For example: There’s no way to detect XP Home vs. XP Professional.
  4. Use WMI to query for information about the OS. This gives you everything you could possibly want all in one shot. Unfortunately, it’s not supported in pre-NT versions of Windows (of course, this is becoming less and less of a problem).

I started with number 1. After pulling most of my hair out and wanting to punch my keyboard, I moved on to number 2. That was a dead end too. I could have made it work, but I didn’t have the patience. I gave number 3 a try and was very happy… …until I decided that the specific version of 2000, XP, Vista, and 7 mattered to me. So I finally ended up with a combination of 3 and 4.

For part 1 of this series, I’m going to focus on number 3, a.k.a. The Easy Way tm

The Code

The key to everything is the Environment class. It has everything we need in a property called OSVersion. However, the information is a little cryptic (mostly a bunch of numbers), so it takes some work to get a “friendly” value returned.

This first method gives us the Operating system architecture as an integer. The environment variable “PROCESSOR_ARCHITECTURE” is either set to “x86” or doesn’t exist on 32-bit versions of Windows. It’s always set to something other than “x86” on 64-bit versions of Windows.

[cc lang=’csharp’]
int getOSArchitecture()
<
string pa = Environment.GetEnvironmentVariable(“PROCESSOR_ARCHITECTURE”);
return ((String.IsNullOrEmpty(pa) || String.Compare(pa, 0, “x86”, 0, 3, true) == 0) ? 32 : 64);
>
[/cc]

For something that seems so simple, this can actually be very confusing. There are two important notes about this method:

  1. Even though the physical CPU‘s architecture may support 64-bit operations, this environment variable will always return the architecture of the OS. So 32-bit Windows running on a 64-bit capable processor will return 32-bit. It’s a bit of a misnomer, but provides the information we really want anyway.
  2. Even though the CPU is 64-bit capable, and the OS architecture is 64-bit, your .Net program could still be running as a 32-bit application. Sometimes, this information is more useful than knowing whether Windows is 32- or 64-bit. To see if your program is actually running in 64-bit mode, check to see if IntPtr.Size == 8. It will be 4 in 32-bit mode and 8 in 64-bit mode.
Читайте также:  Visual studio 2019 windows forms messagebox

Now onto the actual Operating System version. The following code is largely based on what I found on the Microsoft Knowledge Base article: How to determine the Windows version by using Visual C#.

[cc lang=’csharp’]
string getOSInfo()
<
//Get Operating system information.
OperatingSystem os = Environment.OSVersion;
//Get version information about the os.
Version vs = os.Version;

//Variable to hold our return value
string operatingSystem = “”;

if (os.Platform == PlatformID.Win32Windows)
<
//This is a pre-NT version of Windows
switch (vs.Minor)
<
case 0:
operatingSystem = “95”;
break;
case 10:
if (vs.Revision.ToString() == “2222A”)
operatingSystem = “98SE”;
else
operatingSystem = “98”;
break;
case 90:
operatingSystem = “Me”;
break;
default:
break;
>
>
else if (os.Platform == PlatformID.Win32NT)
<
switch (vs.Major)
<
case 3:
operatingSystem = “NT 3.51”;
break;
case 4:
operatingSystem = “NT 4.0”;
break;
case 5:
if (vs.Minor == 0)
operatingSystem = “2000”;
else
operatingSystem = “XP”;
break;
case 6:
if (vs.Minor == 0)
operatingSystem = “Vista”;
else
operatingSystem = “7”;
break;
default:
break;
>
>
//Make sure we actually got something in our OS check
//We don’t want to just return ” Service Pack 2″ or ” 32-bit”
//That information is useless without the OS version.
if (operatingSystem != “”)
<
//Got something. Let’s prepend “Windows” and get more info.
operatingSystem = “Windows ” + operatingSystem;
//See if there’s a service pack installed.
if (os.ServicePack != “”)
<
//Append it to the OS name. i.e. “Windows XP Service Pack 3″
operatingSystem += ” ” + os.ServicePack;
>
//Append the OS architecture. i.e. “Windows XP Service Pack 3 32-bit”
operatingSystem += ” ” + getOSArchitecture().ToString() + “-bit”;
>
//Return the information we’ve gathered.
return operatingSystem;
>
[/cc]

Notice that getOSInfo() returns an empty string if it was unable to determine the OS version.

The code should be pretty self-explanatory, and the great thing is that you don’t have to reference any special assemblies. Everything is right there in System.

Properly detect Windows version in C# .NET – even Windows 10

OK, so you are using System.Environment.OSVersion.Version…

The .NET Framework provides a class to find out the version of Windows. Take a look at the following example:

The output of the above code looks something like this:

This number can be used to identify the operating system. The following table summarizes the most recent operating system version numbers:

Operating system Version number (Major.Minor)
Windows 10 10.0*
Windows Server 2019 10.0*
Windows Server 2016 10.0*
Windows 8.1 6.3*
Windows Server 2012 R2 6.3*
Windows 8 6.2
Windows Server 2012 6.2
Windows 7 6.1
Windows Server 2008 R2 6.1
Windows Server 2008 6.0
Windows Vista 6.0
Windows Server 2003 R2 5.2
Windows Server 2003 5.2
Windows XP 64-Bit Edition 5.2
Windows XP 5.1
Windows 2000 5.0

However, this does not work as desired if the function is executed on the operating systems marked with an asterisk in the table above. Specifically, these are: Windows 10, Windows Server 2019, Windows Server 2016, Windows 8.1, Windows Server 2012 R2

This means for example: for Windows 10 it will return 6.2, which is wrong, as this refers to Windows 8 / Windows Server 2012.

How does this behavior come about? (Note: you can skip the following paragraph if you are not interested in technical background details)

Why OSVersion.Version will return wrong results on newer OSes

In Windows 8.1 and Windows 10, the GetVersion and GetVersionEx functions have been deprecated. In Windows 10, the VerifyVersionInfo function has also been deprecated. While you can still call the deprecated functions, if your application does not specifically target Windows 8.1 or Windows 10, you will get Windows 8 version (6.2.0.0).

In order to target Windows 8.1 or Windows 10, you need to include the app manifest in the source file, which is our first possible solution.

Targeting your application for Windows in the app manifest

In Visual Studio you can add an app manifest by doing the following:

Go to your project, right click and choose Add / New Item and choose Application Manifest File. A new file will be added to your project having default name app.manifest. In the following section you can uncomment Windows 8.1 and 10.

If you run the program again, the output on a Windows 10 will be correct now:

  • Microsoft says: Manifesting the .exe for Windows 8.1 or Windows 10 will not have any impact when run on previous operating systems.
  • The GUIDs in manifest file are commented with Windows desktop versions, but they are also valid for the Windows Server edition. E.g., the GUID for Windows 8.1 also represents Windows Server 2012 R2, as they have the same OS version number 6.3
  • The approach with the manifest file has a major drawback: Every time Microsoft launches a new Windows version, you need to update your application with a new manifest file. Without such an update, your app will not be able to detect the new Windows and will report the number wrong (again).
Читайте также:  Когда будет едадил для windows phone

Calling RtlGetVersion in ntdll.dll

The Windows Kernel offers an interesting function. The RtlGetVersion routine returns version information about the currently running operating system. It is available starting with Windows 2000 and also works on Windows 10/Server 2019/Server 2016 right away.

How to get the Windows release ID (like „1909“) and the update build release (UBR)

Feature updates for Windows 10 are released twice a year, around March and September, via the Semi-Annual Channel. They will be serviced with monthly quality updates for 18 or 30 months from the date of the release, depending on the lifecycle policy.

As a result, Windows 10 has the feature update versions, which are 1909, 1903, 1809, etc. This is referred as Release ID. Within this feature release, Windows gets monthly quality updates.

The current state of this quality updates can be determined using the UBR, which is the last part of the build number (the 778 in „Version 10.0.18363.778“). According to current knowledge, both information can only be read from the registry and there is no Windows API command for this. This is also confirmed by the fact that WINVER also uses the registry.

Check Windows version

How I can check in C++ if Windows version installed on computer is Windows Vista and higher (Windows 7)?

7 Answers 7

Similar to other tests for checking the version of Windows NT:

All the answers in this thread point you to using GetVersion or GetVersionEx for this test, which is incorrect. It seems to work, but it is risky. The primary source of appcompat problems for Windows OS upgrades comes from poorly written tests based on GetVersion results with bad assumptions or buggy comparisons.

The correct way to do this test is to use VerifyVersionInfo , not GetVersion or GetVersionEx .

If you are using the VS 2013 compiler toolset and the Windows 8.1 SDK, you can use the VersionHelpers.h and just call IsWindowsVistaOrGreater .

If you are using the VS 2013 v120_xp platform toolset to target Windows XP, you are actually using the Windows 7.1A SDK, so you need to use VeriyVersionInfo directly.

This code will work on Windows 2000 or later and give you a robust result. If you really needed this test to run on Windows 98 or Windows ME -and- you are using a compiler toolset old enough to actually run on that platform, you’d do the same test but with explicit rather than implicit linking. What’s in a version number?

Furthermore, using GetVersion or GetVersionEx will by default get the wrong version on Windows 8.1 and Windows 10. See Manifest Madness.

Windows version in c# [duplicate]

I want to know which Windows version the PC has.. in C# Framework 3.5

I have tried using

OperatingSystem os = Environment.OSVersion;

Version ver = os.Version;

But the result is

Version minor: 2

The problem is that I have «Windows 8 Pro».

How can I detect it?

4 Answers 4

You will have to match version numbers with the appropriate string value yourself.

Here is a list of the most recent Windows OS and their corresponding version number:

  • Windows Server 2016 Technical Preview — 10.0*
  • Windows 10 — 10.0*
  • Windows 8.1 — 6.3*
  • Windows Server 2012 R2 — 6.3*
  • Windows 8 — 6.2
  • Windows Server 2012 — 6.2
  • Windows 7 — 6.1
  • Windows Server 2008 R2 — 6.1
  • Windows Server 2008 — 6.0
  • Windows Vista — 6.0
  • Windows Server 2003 R2 — 5.2
  • Windows Server 2003 — 5.2
  • Windows XP 64-Bit Edition — 5.2
  • Windows XP — 5.1
  • Windows 2000 — 5.0
Читайте также:  5504 код windows server

*For applications that have been manifested for Windows 8.1 or 10. Applications not manifested for 8.1 / 10 will return the Windows 8 OS version value (6.2).

Also, from the same source:

Identifying the current operating system is usually not the best way to determine whether a particular operating system feature is present. This is because the operating system may have had new features added in a redistributable DLL. Rather than using the Version API Helper functions to determine the operating system platform or version number, test for the presence of the feature itself.

Detect Windows version in .net

How can I detect the Windows OS versions in .net?

What code can I use?

15 Answers 15

System.Environment.OSVersion has the information you need for distinguishing most Windows OS major releases, but not all. It consists of three components which map to the following Windows versions:

For a library that allows you to get a more complete view of the exact release of Windows that the current execution environment is running in, check out this library.

Important note: if your executable assembly manifest doesn’t explicitly state that your exe assembly is compatible with Windows 8.1 and Windows 10.0, System.Environment.OSVersion will return Windows 8 version, which is 6.2, instead of 6.3 and 10.0! Source: here.

I used this when I had to determine various Microsoft Operating System versions:

I use the ManagementObjectSearcher of namespace System.Management

Do not forget to add the reference to the Assembly System.Management.dll and put the using: using System.Management;

Result:

Like R. Bemrose suggested, if you are doing Windows 7 specific features, you should look at the Windows® API Code Pack for Microsoft® .NET Framework.

It contains a CoreHelpers class that let you determine the OS you are currently on (XP and above only, its a requirement for .NET nowaday)

It also provide multiple helper methods. For example, suppose that you want to use the jump list of Windows 7, there is a class TaskbarManager that provide a property called IsPlatformSupported and it will return true if you are on Windows 7 and above.

You can use this helper class;

Sample code is here:

This is a relatively old question but I’ve recently had to solve this problem and didn’t see my solution posted anywhere.

The easiest (and simplest way in my opinion) is to just use a pinvoke call to RtlGetVersion

Where Major and Minor version numbers in this struct correspond to the values in the table of the accepted answer.

This returns the correct Windows version number unlike the deprecated GetVersion & GetVersionEx functions from kernel32

Via Environment.OSVersion which «Gets an System.OperatingSystem object that contains the current platform identifier and version number.»

These all seem like very complicated answers for a very simple function:

Detect OS Version:

Then simply do wrap a select case around the function.

  1. Add reference to Microsoft.VisualBasic .
  2. Include namespace using Microsoft.VisualBasic.Devices ;
  3. Use new ComputerInfo().OSFullName

The return value is «Microsoft Windows 10 Enterprise»

The above answers would give me Major version 6 on Windows 10.

The solution that I have found to work without adding extra VB libraries was following:

I wouldn’t consider this the best way to get the version, but the upside this is oneliner no extra libraries, and in my case checking if it contains «10» was good enough.

How about using a Registry to get the name.

«HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion» has a value ProductName since Windows XP.

If you are using .NET Framework 4.0 or above. You can remove the Is64BitOperatingSystem() method and use Environment.Is64BitOperatingSystem.

First solution

To make sure you get the right version with Environment.OSVersion you should add an app.manifest using Visual Studio and uncomment following supportedOS tags:

Then in your code you can use Environment.OSVersion like this:

Example

For instance in my machine ( Windows 10.0 Build 18362.476 ) result would be like this which is incorrect:

By adding app.manifest and uncomment those tags I will get the right version number:

Alternative solution

If you don’t like adding app.manifest to your project, you can use OSDescription which is available since .NET Framework 4.7.1 and .NET Core 1.0.

Note: Don’t forget to add following using statement at top of your file.

You can read more about it and supported platforms here.

Оцените статью