Environment.OSVersion , as others have argued, is the right way.
However, if someone wants to get it through the registry, this can be used -
using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine .OpenSubKey(@"SOFTWARE\Microsoft\WindowsNT\CurrentVersion")) { var osVersion = key.GetValue("CurrentVersion"); }
Registry for the HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" version HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" .
In addition, the corresponding comparison with the actual OS from here -
Operating system Version number ----------------- -------------- 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
source share