4

I have a problem, How can I check the windows version from the registry in c#?

(Windows xp to windows 8.1)

user2690381
  • 73
  • 1
  • 4
  • 5
    *Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results* – Federico Berasategui Aug 16 '13 at 18:41
  • possible duplicate of [How to get the "friendly" OS Version Name?](http://stackoverflow.com/questions/577634/how-to-get-the-friendly-os-version-name) – Oliver Gray Aug 16 '13 at 18:44
  • 1
    My question, Why do you check registry for version? can't you get it from `Environment` class? – Sriram Sakthivel Aug 16 '13 at 18:45

3 Answers3

7

Environment.OSVersion can give you that!

Read the MSDN documentation for Environment class to see all of the other things you can get from this class.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
Ehsan
  • 31,833
  • 6
  • 56
  • 65
6

Environment.OSVersion as others stated is right way to go.

However, in case someone want to get it through 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 version is HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion".

Also, corresponding mapping with 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
Community
  • 1
  • 1
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
1

You don't need to read the registry; System.Environment.OSVersion gives you this information.

Varun K
  • 3,593
  • 2
  • 25
  • 26