Home > Programming > C# Detect Windows OS Version - Part 1

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 Waytm

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.

1
2
3
4
5
int getOSArchitecture()
{
    string pa = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
    return ((String.IsNullOrEmpty(pa) || String.Compare(pa, 0, "x86", 0, 3, true) == 0) ? 32 : 64);
}

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.

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#.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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;
}

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.


Want even more information? Don't miss Part 2: Using WMI


  • Marc W.

    There is a slight bug in the above code when checking for the Windows version, I ran into this when I'm checking.

    Version 6.x.x.x can be Vista, Windows Server 2008, or Windows 7 (I don't get it).

    In this case, I check the "WindowsName" and check if it contains "Server", "Vista", or "Windows 7"

  • http://andrewensley.com/ Andrew

    @Marc W.
    Right you are. That is why I check for:

    vs.Minor == 0

    Vista reports version 6.0.x.x. Windows 7 reports version 6.1.x.x.

    As for Windows Server, this code does not consider Windows Server versions at all. It is simply for client OSes. I guess I could make the description clearer. Sorry for any confusion.

    From your description, I'm not sure what the bug is in my code. Maybe it's that I wasn't checking for server versions?

  • Matt B.

    This code really helped me out - I'm trying to make a toolkit similar to Dial-A-Fix for my work, and I needed a way to disable options according to OS. Thanks!

  • http://andrewensley.com/ Andrew

    @Matt B.
    I'm glad it was useful. :-)

  • http://peitor.blogspot.com/ Peter Gfader

    @MattB
    As I said on my blog I wouldn't test for the OS version but for the available feature!
    See http://peitor.blogspot.com/2009/08/check-for-feature-and-not-version.html

    @Andrew
    Thanks for your comment on my blog!
    Why didn't you use the code from http://www.csharp411.com/determine-windows-version-and-edition-with-c/
    Any reason?
    However I am looking forward to see the WMI code...

    Thanks
    .peter.gfader.

  • http://andrewensley.com/ Andrew

    @Peter Gfader
    Three reasons I didn't use that other code:

    1. I didn't know about it. :-p
    2. It's far more complex than mine (look at the source code the author provides a link for)
    3. It doesn't account for Windows 7

    I'm hoping to post the WMI code soon. Work has had me pretty busy lately. :-)

  • http://www.chrispaul.ws Chris

    Thanks for sharing this! I am looking forward to your WMI code... perhaps you can leave a comment on this post when you do...

  • http://www.xnode-online.com Bacardi

    Great work, thanks for sharing!

  • Thomas W.

    Well, Windows 7 - but which edition?
    All the servers are missing as well (2003 Server, 2008 Server) including the editions.
    This one tries but is not up to date regarding Windows 7: http://netcode.ru/dotnet/?lang=&katID=30&skatID=277&artID=7667

    • http://andrewensley.com/ Andrew

      You are correct. This bit of code does not account for Server OSes or the different editions of 2000, XP, Vista, or 7. It will only tell you the base OS name. Part 2 (the WMI code) will give you the level of granularity you're looking for. I haven't had much free time lately, but I really hope to post the WMI code soon.

  • Thomas W.

    Based on above link with this additional information you can modify the sources for Windows 7: http://www.codeguru.com/cpp/misc/misc/system/article.php/c8973/

    • http://andrewensley.com/ Andrew

      Thanks for the link. The WMI code I will post soon will give you all of that information without having to decode the "Product Type" numbers.

  • Pingback: C# Detect Windows OS Version – Part 2 (WMI) | AndrewEnsley.com

  • Pingback: C# Detect Windows OS Version – Part 2 (WMI) | AndrewEnsley.com

  • MXM

    i copied the code exactly like its shown and i get an error on the line (marked here as #72)

    operatingSystem += " " + getOSArchitecture().ToString() + "-bit";

    i believe im missing something, i managed to adecuate the code in order to work for me (since all i need is differentiate between XP and Vista) but I was wondering what should I add to this code in order to get the value as shown here and sweep the error?

    eror:Error 1 The name 'getOSArchitecture' does not exist in the current context

    thanks.

    peace.

    • http://andrewensley.com/ Andrew

      Looks like the problem is pretty simple. Did you copy both sections of code? The getOSArchitecture() method is defined in the first section.

      Also, to make things a little easier, you can go to Part 2: Using WMI and download the source code which includes the WMI method, and a fallback to this code in case WMI isn't available.

  • Cesar

    Yes, it was useful. Thanks!

    • http://andrewensley.com/ Andrew

      You're welcome!

  • Darshit

    Thanks,

    This helped me to identify desktop folder for user as they are different in Windows XP and Windows7/Vista.

  • Robat7

    Thank a lot,
    it help me on how to toggle Desktop in diffent version of Windows

  • http://cartransmission.inube.com/ 700R4 Transmission

    Bravo, Bros! keep going like this, more good info again.

  • http://twitter.com/MossyBlog Scott Barnes

    You get nuke the getOSArchitecture() with:

    operatingSystem += " " + (Environment.Is64BitOperatingSystem ? 64 : 32) +"-bit";

    • http://andrewensley.com/ Andrew

      Good catch! That is true.

      However, the Environment.Is64BitOperatingSystem Property was only introduced in .Net 4.0.  My code is compatible with any .Net version.

      As long as you know your client will be running .Net 4.0, though, go ahead and use the much simpler Environment property.

  • http://www.nolme.com Vincent Duvernet

    Here's my results from several computers :
    (Major.Minor.RevisionMajor.RevisionMinor)

    Windows 2k SP4 x86 Fr     : 5.0.4.0 build 2195 revision 262144Windows 2k SP4 x86 Eng   : 5.0.4.0 build 2195 revision 262144
    Windows XP SP1 x64 Eng   : 5.2.1.0 build 3790 revision 65536Windows XP SP2 x64 Eng   : 5.2.2.0 build 3790 revision 131072Windows XP SP2 x86 Fr     : 5.1.2.0 build 2600 revision 131072
    Windows XP SP3 x86 Fr     : 5.1.3.0 build 2600 revision 196608Windows Vista  SP1 x86 Fr : 6.0.1.0 build 6001 revision 65536
    Windows 7  SP1 x86 Fr      : 6.1.1.0 build 7601 revision 65536Windows 7  SP1 x64 Fr      : 6.1.1.0 build 7601 revision 65536
    ++
    Vincent

    • http://www.nolme.com Vincent Duvernet

      (sorry the format get away :/)

  • Werner Sandner

    Thank you, you saved me time

  • http://twitter.com/_CoreyHoward Corey Howard

    System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", EnvironmentVariableTarget.Machine)
    Returns the correct value.

    Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE"); was only returning 32bit.

  • Pingback: [SRC]Ghetto Get Windows Version

  • Pedro

    Fantastic, thank you very much. You have just done like 20 min of work for me.

privacy