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.

[cc lang=’csharp’]
int getOSArchitecture()
{
string pa = Environment.GetEnvironmentVariable(“PROCESSOR_ARCHITECTURE”);
return ((String.IsNullOrEmpty(pa) || String.Compare(pa, 0, “x86”, 0, 3, true) == 0) ? 32 : 64);
}
[/cc]

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

[cc lang=’csharp’]
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;
}
[/cc]

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


blog

Andrew

Programmer / Musician / Soccer Player

32 thoughts to “C# Detect Windows OS Version – Part 1”

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

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

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

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

  7. System.Environment.GetEnvironmentVariable(“PROCESSOR_ARCHITECTURE”, EnvironmentVariableTarget.Machine)
    Returns the correct value.

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

Leave a Reply

Your email address will not be published. Required fields are marked *