30

It seems that the word "version" in reference to Windows is used for different things. For example, the Windows 10 "Anniversary Update" is labeled "Version 1607" by Microsoft (here for example). But if I try to get the "Version" (on a PC with the Anniversary Update installed) using the following code, nothing is returned that looks like "1607".

// Get Version details
Version ver = os.Version;
Console.WriteLine("Major version: " + ver.Major);
Console.WriteLine("Major Revision: " + ver.MajorRevision);
Console.WriteLine("Minor version: " + ver.Minor);
Console.WriteLine("Minor Revision: " + ver.MinorRevision);
Console.WriteLine("Build: " + ver.Build);

I get this:

Major version: 6
Major Revision: 0
Minor version: 2
Minor Revision: 0
Build: 9200

How do I get the Windows 10 "version" as in "Version 1607"?

Thanks!

Joe Gayetty
  • 1,521
  • 2
  • 22
  • 35
  • If you want Environment.OSVersion to reflect the correct Windows Build version, see answer about creating an app manifest file here https://stackoverflow.com/questions/40672109/get-current-version-os-in-windows-10-in-c-sharp . May be more useful than the year-and-month style version number in case the user is getting Insider Updates – foson Dec 19 '18 at 14:52

4 Answers4

42

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);
Scott Perham
  • 2,410
  • 1
  • 10
  • 20
Stavm
  • 7,833
  • 5
  • 44
  • 68
  • 3
    Excellent! Verified to work on my PC with "Windows 10 version 1511" and another PC with "version 1607" -- which is what was expected. – Joe Gayetty Sep 29 '16 at 20:17
  • 3
    @Stavm — great info. BTW, Microsoft **does** refer to the _ReleaseId_ (e.g., "1803") as a Version, see [Windows 10 release information](https://www.microsoft.com/en-us/itpro/windows-10/release-information) for details. – Ken May 01 '18 at 21:29
  • In addition, the original (RTM) version of Windows 10 does **not** have this key (just like Windows 7 or other windows versions) – gog May 17 '19 at 11:53
  • On Win7, string releaseId returns empty. – Jonney Aug 15 '20 at 08:11
  • 2
    this no longer works. microsoft broke releaseid for 21h1. you'd have to use diplayversion instead for 21h1, which might also break in the future and wont work for older oses. – az1d May 28 '21 at 07:19
5
string Version = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows NT\CurrentVersion", "ProductName", null);

Gives a name like "Windows 10 Enterprise".

Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
Skyfish
  • 119
  • 2
  • 4
  • 1
    This was already posted as an answer a few months ago [here](https://stackoverflow.com/a/49835788/1364007). – Wai Ha Lee Jan 25 '19 at 16:18
3
 private static ManagementObject GetMngObj(string className)
    {
        var wmi = new ManagementClass(className);

        foreach (var o in wmi.GetInstances())
        {
            var mo = (ManagementObject)o;
            if (mo != null) return mo;
        }

        return null;
    }

    public static string GetOsVer()
    {
        try
        {
            ManagementObject mo = GetMngObj("Win32_OperatingSystem");

            if (null == mo)
                return string.Empty;

            return mo["Version"] as string;
        }
        catch (Exception e)
        {
            return string.Empty;
        }
    }

How to Use:

Console.WriteLine(GetOsVer());

Result: 10.0.0.1299

nap
  • 41
  • 3
2

In addition to Scott's answer, you can also get the product name (ex. Windows 10 Pro) with this (*I take no credit as Scott is the one who mentioned the registry path + I'm reusing his code below):

using Microsoft.Win32;

string ProductName = 
Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ProductName", "").ToString();
Console.WriteLine(ProductName);
  • Dealing with 32 / 64 bits; 32 bits application in 64bits OS can retrieve wrong values. Check this thread [stackLink](https://stackoverflow.com/questions/13728491/opensubkey-returns-null-for-a-registry-key-that-i-can-see-in-regedit-exe) – antonio Nov 19 '21 at 05:06