Detect Windows/Operating System Version in C# .NET

Are you looking for a way to detect which OS you are running on in your .NET application? I know I had to so that I could implement workarounds that were OS specific.

Here is a simple function to get the name. You can use this as an idea of how to pick the exact version you are on.

Please don’t be one of those morons that uses this function then does a string comparison on the return. Use common sense and extract the logic from this to get what you need.

I better not see any code out there like

if(GetOSVersionName() == "Windows 98"){Do stupid stuff here}

Yes, it would technically work, so I have no doubt it will be implemented that way by some of you. Religious debate? Probably.

[csharp]
 public string GetOSVersionName()
        {
            string strVersion = "Unknown";
            switch (Environment.OSVersion.Platform)
            {
                case PlatformID.Win32S:
                    return "Windows 3.1";
                case PlatformID.Win32Windows:
                    switch (Environment.OSVersion.Version.Minor)
                    {
                        case 0:
                            return "Windows 95";
                        case 10:
                            if (Environment.OSVersion.Version.Revision.ToString() == "2222A")
                            {
                                return "Windows 98 Second Edition";
                            }
                            else
                                return "Windows 98";
                        case 90:
                            return "Windows ME";
                    }
                    break;
                case PlatformID.Win32NT:
                    switch (Environment.OSVersion.Version.Major)
                    {
                        case 3:
                            return "Windows NT 3.51";
                        case 4:
                            return "Windows NT 4.0";
                        case 5:
                            switch (Environment.OSVersion.Version.Minor)
                            {
                                case 0:
                                    return "Windows 2000";
                                case 1:
                                    return "Windows XP";
                                case 2:
                                    return "Windows 2003";
                            }
                            break;
                        case 6:
                            switch (Environment.OSVersion.Version.Minor)
                            {
                                case 0:
                                    return "Windows Vista";
                                case 1:
                                    return "Windows 2008";
                                case 2:
                                    return "Windows 7";
                            }
                            break;
                    }
                    break;
                case PlatformID.WinCE:
                    return "Windows CE";
                case PlatformID.Unix:
                    return "Unix";
            }
            return strVersion;
        }
[/csharp]

Happy Coding!

One Reply to “Detect Windows/Operating System Version in C# .NET”

  1. Win7 and windows 2008 versions are the wrong way round. Handy code though! I converted it to return enum as better for return value processing.

Leave a Reply

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