address
Home > Programming > C# Detect Windows OS Version – Part 1

C# Detect Windows OS Version – Part 1

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.

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.

Part 2: Using WMI

Related posts:

  1. C# Detect Windows OS Version – Part 2 (WMI)
  2. RFC Standard for the transmission of IP datagrams on Avian Carriers
  3. Install Ejabberd 2.0 on Ubuntu Hardy Heron 8.04 LTS – 6 Steps!
  1. Marc W.
    August 13th, 2009 at 14:19 | #1

    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″

  2. August 13th, 2009 at 15:27 | #2

    @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?

  3. Matt B.
    August 17th, 2009 at 01:37 | #3

    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!

  4. August 17th, 2009 at 11:47 | #4

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

  5. August 21st, 2009 at 04:44 | #5

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

  6. August 21st, 2009 at 09:27 | #6

    @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. :-)

  7. September 16th, 2009 at 17:24 | #7

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

  8. October 1st, 2009 at 12:29 | #9

    Great work, thanks for sharing!

  9. Thomas W.
    October 5th, 2009 at 03:59 | #10

    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

    • October 6th, 2009 at 01:23 | #11

      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.

  10. Thomas W.
    October 5th, 2009 at 04:19 | #12

    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/

    • October 6th, 2009 at 01:27 | #13

      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.

  11. MXM
    March 3rd, 2010 at 10:03 | #14

    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.

    • March 3rd, 2010 at 10:25 | #15

      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.

  12. Cesar
    July 28th, 2010 at 07:52 | #16

    Yes, it was useful. Thanks!

  1. October 7th, 2009 at 23:57 | #1
  2. October 7th, 2009 at 23:57 | #2
feed
help
report