Click here to Skip to main content
16,018,353 members
Articles / Desktop Programming / Windows Forms

Getting Operating System Version Info - Even for Windows 10!

,
Rate me:
Please Sign up or sign in to vote.
4.92/5 (75 votes)
20 Nov 2012CPOL4 min read 404.8K   19.7K   115   114
Get the operating system version and edition, updated with Windows 10

 

OSVersionInfo

Introduction

I was looking for a way to determine the version of the operating system my program was running under. When I Googled it, I got a lot of code hits, but they all had the same problem: They were not updated for Windows 7. 

Also, all of them had the same two shortcomings:

  1. They didn't include all available Windows editions (especially the absence of "Professional" bothered me as that is the operating system edition I normally use)
  2. I wanted to know if the operating system was 32 bit or 64 bit, and none of the solutions I found correctly determined that. Much more on that later...

Background

I found this article http://www.csharp411.com/determine-windows-version-and-edition-with-c/ and that was in my opinion the best and most updated version. So I decided to simply amend that code and add the things that were missing.

Please note: ALL CREDIT FOR THE ORIGINAL CODE GOES TO "TIMM", THE AUTHOR OF THE ABOVEMENTIONED ARTICLE...

Changes Made By Me

  1. I added "Windows 7" and "Windows Server 2008 R2" to the detection scheme.
  2. I added all the missing Windows editions I could find.
  3. Completely rewrote the 32/64 bit detection code.

Using the Code

The class I came up with is very easy to use. Just include the CS file in your project (or compile it to a DLL for use in your VB project), and query the properties like this:

C#
StringBuilder sb = new StringBuilder(String.Empty);
sb.AppendLine("Operation System Information");
sb.AppendLine("----------------------------");
sb.AppendLine(String.Format("Name = {0}", OSVersionInfo.Name));
sb.AppendLine(String.Format("Edition = {0}", OSVersionInfo.Edition));
if (OSVersionInfo.ServicePack!=string.Empty)
   sb.AppendLine(String.Format("Service Pack = {0}", OSVersionInfo.ServicePack));
else
   sb.AppendLine("Service Pack = None");
sb.AppendLine(String.Format("Version = {0}", OSVersionInfo.VersionString));
sb.AppendLine(String.Format("ProcessorBits = {0}", OSVersionInfo.ProcessorBits));
sb.AppendLine(String.Format("OSBits = {0}", OSVersionInfo.OSBits));
sb.AppendLine(String.Format("ProgramBits = {0}", OSVersionInfo.ProgramBits));

textBox1.Text = sb.ToString();

Points of Interest

The big problem with this was actually the detection of whether or not your OPERATING SYSTEM is 32 or 64 bits. As mentioned, I found a lot of suggestions to how this could be detected, but none of them worked properly. For those who are interested and for the sake of learning/sharing information, I'll list the different suggestions here:

  1. Using the IntPtr size

    The most popular method seems to be variations on this:

    C#
    return IntPtr.Size * 8;

    But this doesn't actually return the Bit architecture of the OS, it returns the bit value for the running program. So for programs running in 32 bit mode on 64 bit Windows, the above code will return 32.

  2. Using the 'PROCESSOR_ARCHITECTURE' Environment variable:
    C#
    string pa = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
    return ((String.IsNullOrEmpty(pa) || String.Compare(pa, 0,
        "x86", 0, 3, true) == 0) ? 32 : 64);

    This is actually very misleading, because the result of this is exactly the same as version 1: It doesn't return the PROCESSOR bit architecture as the name says, but the bit architecture of the running program. For programs running in 32 bit mode on 64 bit Windows, the above code will ALSO return 32.

  3. Using PInvoke and GetSystemInfo

    Please note: To keep the article at a reasonable length, I'm not including the Structure declarations and the PInvoke API declarations... They can be found in the source code.

    C#
    ProcessorArchitecture pbits = ProcessorArchitecture.Unknown;
    try
    {
        SYSTEM_INFO l_System_Info = new SYSTEM_INFO();
        GetSystemInfo(ref l_System_Info);
        switch (l_System_Info.uProcessorInfo.wProcessorArchitecture)
        {
            case 9: // PROCESSOR_ARCHITECTURE_AMD64
                pbits = ProcessorArchitecture.Bit64;
                break;
    
            case 6: // PROCESSOR_ARCHITECTURE_IA64
                pbits = ProcessorArchitecture.Itanium64;
                break;
    
            case 0: // PROCESSOR_ARCHITECTURE_INTEL
                pbits = ProcessorArchitecture.Bit32;
                break;
    
            default: // PROCESSOR_ARCHITECTURE_UNKNOWN
                pbits = ProcessorArchitecture.Unknown;
                break;
        }
    }
    catch
    {
        Ignore 
    }
    return pbits;

    Once again, I was disappointed. This code - despite the presence of the processor specific flags - ALSO returned the bits of the running program, not the OS and not the processor.

  4. Using PInvoke and GetNativeSystemInfo

    I read somewhere that the above was not to be trusted (as I had already discovered), and that you should use the GetNativeSystemInfo API instead.

    The code is exactly the same as the above, but GetSystemInfo is replaced by GetNativeSystemInfo, and the same in the API declaration.

    NOW I got another result. But alas, it seemed that this API actually returns the bit architecture of the processor itself. And I was interested in the OS bit architecture. You can easily have a 32 bit windows version running on a 64 bit processored machine.

    So I was still not done.

    After A LOT of research, I found the method I decided to use in the class:

  5. A combination of IntPtr.Size and IsWow64Process:
    C#
    static public SoftwareArchitecture OSBits
    {
        get
        {
            SoftwareArchitecture osbits = SoftwareArchitecture.Unknown;
    
            switch (IntPtr.Size * 8)
            {
                case 64:
                    osbits = SoftwareArchitecture.Bit64;
                    break;
    
                case 32:
                    if (Is32BitProcessOn64BitProcessor())
                        osbits = SoftwareArchitecture.Bit64;
                    else
                        osbits = SoftwareArchitecture.Bit32;
                    break;
    
                default:
                    osbits = SoftwareArchitecture.Unknown;
                    break;
            }
    
            return osbits;
        }
    }
    
    private static IsWow64ProcessDelegate GetIsWow64ProcessDelegate()
    {
        IntPtr handle = LoadLibrary("kernel32");
    
        if (handle != IntPtr.Zero)
        {
            IntPtr fnPtr = GetProcAddress(handle, "IsWow64Process");
    
            if (fnPtr != IntPtr.Zero)
            {
                return (IsWow64ProcessDelegate)Marshal.GetDelegateForFunctionPointer((IntPtr)fnPtr,
                        typeof(IsWow64ProcessDelegate));
            }
        }
    
        return null;
    }
    
    private static bool Is32BitProcessOn64BitProcessor()
    {
        IsWow64ProcessDelegate fnDelegate = GetIsWow64ProcessDelegate();
    
        if (fnDelegate == null)
        {
            return false;
        }
    
        bool isWow64;
        bool retVal = fnDelegate.Invoke(Process.GetCurrentProcess().Handle, out isWow64);
    
        if (retVal == false)
        {
            return false;
        }
    
        return isWow64;
    }

    If the IntPtr size is 64, then the OS MUST be 64 bits as well because you can't run a 64 bit program on a 32 bit OS.

    If the program is running as 32 bits, then the code checks the process the code runs in to determine if that's 32 or 64 bits.

    If it is 64, then the OS will be 64 bits, but the program is running as 32 bits. And if it's 32 then the OS is also 32 bits.

    In the end, I included most of these methods in the final class lib, because it can be nice to be able to distinguish between the bit architecture of the Program, the OS and the Processor.

Acknowledgements

Thanks to Member 7861383, Scott Vickery for the Windows 8.1 update and workaround.
Thanks to Brisingr Aerowing for help with the Windows 10 adaptation.

History 

  • 2016-02-11 - Version 3.0, Added code for Windows 8.1 & Windows 10
  • 2012-11-21 - Version 2.0, Added version numbers for Windows 8 & Windows Server 2012 
  • 2010-04-15 - Version 1.0, Initial release  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Sweden Sweden
Born in Copenhagen, Denmark
Have been living in Paris, France and L.A., The United States
Now live in Stockholm, Sweden

Started programming when I got my first VIC 20, and a few months later on Commodore 64. Those were the days!

Studied programming at the Copenhagen Engineering Academy

Professional console, winforms and webforms programming in Comal, x86 Assembler, Fortran, Pascal, Delphi, Visual Basic 3 through 6, Classic ASP, C# and VB.NET

I now work as Senior Microsoft Dynamics AX and .Net programmer, and have a number of projects in various states of progress to work on in the spare time...

Written By
Founder GryphonSoft Technologies
United States United States
I am a student at Ivy Tech Community College in Lafayette, Indiana, majoring in Computer Information Systems. My hobbies are programming computers, playing video games, and sleeping.

My real name is Zachary Greve.

I am also a furry (e.g. a person who likes to believe they are some kind of creature), and my fursona (my creature 'form') is a Gryphon. Sadly, I cannot draw worth a flip.

I like Gryphons.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Tonnvb3-Feb-21 12:44
Tonnvb3-Feb-21 12:44 
QuestionEnvironment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE", EnvironmentVariableTarget.Machine) -- works Pin
aodennison19-Sep-17 7:29
aodennison19-Sep-17 7:29 
BugSmall bug for Windows 10, Edition field empty Pin
weekay499-Sep-17 0:11
weekay499-Sep-17 0:11 
GeneralRe: Small bug for Windows 10, Edition field empty Pin
Johnny J.10-Sep-17 21:05
professionalJohnny J.10-Sep-17 21:05 
Bugthere is a bug for windows 10 home edition. Pin
kevin_fly16-Aug-17 17:21
kevin_fly16-Aug-17 17:21 
BugDont work in xp 64 bits Pin
Member 91905619-Jun-17 6:55
Member 91905619-Jun-17 6:55 
GeneralRe: Dont work in xp 64 bits Pin
Brisingr Aerowing21-Jun-17 14:03
professionalBrisingr Aerowing21-Jun-17 14:03 
AnswerTo get this working with Windows 10 properly... Pin
Vader_Oz18-May-17 19:35
Vader_Oz18-May-17 19:35 
PraiseThanks, awesome Pin
rvanpassel11-May-17 4:23
rvanpassel11-May-17 4:23 
PraiseSuper! Pin
Jonathan Reis21-Dec-16 8:32
Jonathan Reis21-Dec-16 8:32 
GeneralRe: Super! Pin
Johnny J.21-Dec-16 21:03
professionalJohnny J.21-Dec-16 21:03 
QuestionExtending this to provide 'VersionHelpers' Functionality ? Pin
Garth J Lancaster23-Aug-16 3:25
professionalGarth J Lancaster23-Aug-16 3:25 
QuestionWindows 10 Anniversary Update Pin
Vader_Oz9-Aug-16 16:02
Vader_Oz9-Aug-16 16:02 
AnswerRe: Windows 10 Anniversary Update Pin
Vader_Oz9-Aug-16 16:09
Vader_Oz9-Aug-16 16:09 
GeneralRe: Windows 10 Anniversary Update Pin
Johnny J.9-Aug-16 20:24
professionalJohnny J.9-Aug-16 20:24 
GeneralRe: Windows 10 Anniversary Update Pin
JJakaJonas6-Feb-17 8:39
JJakaJonas6-Feb-17 8:39 
AnswerRe: Windows 10 Anniversary Update Pin
Vader_Oz17-May-17 19:01
Vader_Oz17-May-17 19:01 
QuestionChanges Pin
Freddyvdh8-Aug-16 22:30
Freddyvdh8-Aug-16 22:30 
AnswerRe: Changes Pin
Johnny J.8-Aug-16 22:57
professionalJohnny J.8-Aug-16 22:57 
QuestionNot working properly with Windows 10 Pin
Manuraj Bharall22-Dec-15 3:08
Manuraj Bharall22-Dec-15 3:08 
AnswerRe: Not working properly with Windows 10 Pin
Johnny J.22-Dec-15 3:42
professionalJohnny J.22-Dec-15 3:42 
GeneralRe: Not working properly with Windows 10 Pin
Brisingr Aerowing26-Dec-15 19:14
professionalBrisingr Aerowing26-Dec-15 19:14 
GeneralRe: Not working properly with Windows 10 Pin
Johnny J.27-Dec-15 21:27
professionalJohnny J.27-Dec-15 21:27 
GeneralRe: Not working properly with Windows 10 Pin
Johnny J.27-Dec-15 21:49
professionalJohnny J.27-Dec-15 21:49 
GeneralRe: Not working properly with Windows 10 Pin
Brisingr Aerowing3-Jan-16 16:47
professionalBrisingr Aerowing3-Jan-16 16:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.