Detect Windows version in .NET
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:
+------------------------------------------------------------------------------+
| | PlatformID | Major version | Minor version |
+------------------------------------------------------------------------------+
| Windows 95 | Win32Windows | 4 | 0 |
| Windows 98 | Win32Windows | 4 | 10 |
| Windows Me | Win32Windows | 4 | 90 |
| Windows NT 4.0 | Win32NT | 4 | 0 |
| Windows 2000 | Win32NT | 5 | 0 |
| Windows XP | Win32NT | 5 | 1 |
| Windows 2003 | Win32NT | 5 | 2 |
| Windows Vista | Win32NT | 6 | 0 |
| Windows 2008 | Win32NT | 6 | 0 |
| Windows 7 | Win32NT | 6 | 1 |
| Windows 2008 R2 | Win32NT | 6 | 1 |
| Windows 8 | Win32NT | 6 | 2 |
| Windows 8.1 | Win32NT | 6 | 3 |
+------------------------------------------------------------------------------+
| Windows 10 | Win32NT | 10 | 0 |
+------------------------------------------------------------------------------+
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.
Update: In .NET 5.0 and later, System.Environment.OSVersion
always returns the actual OS version. For more information, see Environment.OSVersion returns the correct operating system version.
How to check windows version in C# on .NET 5.0 console app
You can use what you need from this (in addition to OSVersion
you can use osName
and osRelease
) :
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using Microsoft.Win32;
string NL = Environment.NewLine;
string HKLMWinNTCurrent = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion";
string osName = get(() => Registry.GetValue(HKLMWinNTCurrent, "productName", "").ToString());
string osRelease = get(() => Registry.GetValue(HKLMWinNTCurrent, "ReleaseId", "").ToString());
if ( !osRelease.IsNullOrEmpty() ) osRelease = $" ({ osRelease})";
string osVersion = Environment.OSVersion.Version.ToString();
string osType = Environment.Is64BitOperatingSystem ? "64-bits" : "32-bits";
string clr = Environment.Version.ToString();
string dotnet = get(() =>
{
var attributes = Assembly.GetExecutingAssembly().CustomAttributes;
var result = attributes.FirstOrDefault(a => a.AttributeType == typeof(TargetFrameworkAttribute));
return result == null
? ".NET Framework (unknown)"
: result.NamedArguments[0].TypedValue.Value.ToString();
});
string Platform = $"{osName} {osType} {osVersion}{osRelease}{NL}{dotnet}{NL}CLR {clr}";
string get(Func<string> func)
{
try { return func(); }
catch { return "(undefined)"; }
}
Example
Windows 10 Pro 64-bits 6.2.9200.0 (2009)
.NET Framework 4.7.2
CLR 4.0.30319.42000
I never noticed that 6.2.9200 is incorrect... !?
Solution from @nap: How to get Windows Version - as in "Windows 10, version 1607"?
Explanation from @DanielDiPaolo: Detect Windows version in .net
How to recognize Windows OS version?
System.Environment.Version
will retrieve the Version of the .NET Framework used to execute your code, so this will not help you.
The System.Environment.OSVersion
object (OperatingSystem class) is indeed the correct way to get the Windows version. You should however do something like this:
public static bool IsWindowsCurrentGen()
{
if (Environment.OSVersion.Version.Major >= 6)
return true;
else
return false;
}
This will return true
for everything above (including) Windows Vista.
For a "full" reference table see this StackOverflow question. Note, that it would be better from my point of view, to detect the installed Excel version rather than the OS version.
How to get Windows Version - as in Windows 10, version 1607 ?
according to MSDN official link there's a specific version number for each windows version out there. in dot net this can be read using the Environment.OSVersion object.
Console.WriteLine("OSVersion: {0}", Environment.OSVersion);
//output: OSVersion: Microsoft Windows NT 6.2.9200.0
What you are looking for is called ReleaseID not a version of windows.
this be can read from registry key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ReleaseId
using Microsoft.Win32;
string releaseId = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ReleaseId", "").ToString();
Console.WriteLine(releaseId);
Get current version OS in Windows 10 in C#
Add application manifest to your application and add the supportedOS Id of Windows 8.1 and Windows 10 to the manifest:
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
</application>
</compatibility>
Now Environment.OSVersion
includes the correct data for Windows 8.1 and Windows 10 and not 6.2 to indicate you run Windows 8. This is a change since Windows 8.1.
How to detect Windows 64-bit platform with .NET?
UPDATE: As Joel Coehoorn and others suggest, starting at .NET Framework 4.0, you can just check Environment.Is64BitOperatingSystem
.
IntPtr.Size won't return the correct value if running in 32-bit .NET Framework 2.0 on 64-bit Windows (it would return 32-bit).
As Microsoft's Raymond Chen describes, you have to first check if running in a 64-bit process (I think in .NET you can do so by checking IntPtr.Size), and if you are running in a 32-bit process, you still have to call the Win API function IsWow64Process. If this returns true, you are running in a 32-bit process on 64-bit Windows.
Microsoft's Raymond Chen:
How to detect programmatically whether you are running on 64-bit Windows
My solution:
static bool is64BitProcess = (IntPtr.Size == 8);
static bool is64BitOperatingSystem = is64BitProcess || InternalCheckIsWow64();
[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWow64Process(
[In] IntPtr hProcess,
[Out] out bool wow64Process
);
public static bool InternalCheckIsWow64()
{
if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) ||
Environment.OSVersion.Version.Major >= 6)
{
using (Process p = Process.GetCurrentProcess())
{
bool retVal;
if (!IsWow64Process(p.Handle, out retVal))
{
return false;
}
return retVal;
}
}
else
{
return false;
}
}
Related Topics
Run Async Method Regularly with Specified Interval
All Possible Combinations of a List of Values
C# Switch Statement Limitations - Why
How to Change Datatype of a Datacolumn in a Datatable
How to Use a C# Keyword as a Property Name
How to Call an Async Method from a Getter or Setter
How to Safely Call an Async Method in C# Without Await
What's the Difference Between Dynamic (C# 4) and Var
JSON.Net Throws Stackoverflowexception When Using [JSONconvert()]
How to Apply Orderby on an Iqueryable Using a String Column Name Within a Generic Extension Method
Async/Await With/Without Awaiting (Fire and Forget)
Fire and Forget Async Method in ASP.NET MVC