23

I have to detect Windows 8 Operating system in my C# Windows Application and do some settings. I know we can detect Windows 7 using Environment.OSVersion, but how can windows 8 be detected?

Thanks in advance.

Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42

5 Answers5

34
Version win8version = new Version(6, 2, 9200, 0);

if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
    Environment.OSVersion.Version >= win8version)
{
    // its win8 or higher.
}

Update for windows 8.1 and higher:

Okay guys, it seems to me that this piece of code has been marked as deprecated by Microsoft itself. I leave a link here so you could read more about it.

In short, it says:

For windows 8 and higher, there always will be the same version number of (6, 2, 9200, 0). And rather than looking for Windows version, go look for the actual feature which existance you are trying to resolve.

Community
  • 1
  • 1
AgentFire
  • 8,944
  • 8
  • 43
  • 90
13

Windows 8 or more recent:

bool IsWindows8OrNewer()
{
    var os = Environment.OSVersion;
    return os.Platform == PlatformID.Win32NT && 
           (os.Version.Major > 6 || (os.Version.Major == 6 && os.Version.Minor >= 2));
}
Asik
  • 21,506
  • 6
  • 72
  • 131
  • Should actually be: return os.Platform == PlatformID.Win32NT && (os.Version.Major > 6 || (os.Version.Major == 6 && os.Version.Minor >= 2)); – Charlie Sep 14 '13 at 00:54
  • Why dont just use the `> or < or == or <= etc` operators of `Version` struct? – AgentFire Sep 15 '13 at 10:22
  • 2
    Because I wasn't aware of these when I wrote the answer, and shortly after you provided an answer using these. Your answer is better but I left mine as an alternative option. – Asik Sep 15 '13 at 14:56
3

Check the answer to the following question: How to get the "friendly" OS Version Name?

Quoted answer:

You can use WMI to get the product name ("Microsoft® Windows Server® 2008 Enterprise "):

using System.Management;
var name = (from x in new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem").Get().OfType<ManagementObject>() select x.GetPropertyValue("Caption")).First();
return name != null ? name.ToString() : "Unknown";
Community
  • 1
  • 1
Christian Westman
  • 2,985
  • 1
  • 26
  • 28
2

Start by declaring a struct as follows:

[StructLayout(LayoutKind.Sequential)]
public struct OsVersionInfoEx
{
    public int dwOSVersionInfoSize;
    public uint dwMajorVersion;
    public uint dwMinorVersion;
    public uint dwBuildNumber;
    public uint dwPlatformId;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
    public string szCSDVersion;
    public UInt16 wServicePackMajor;
    public UInt16 wServicePackMinor;
    public UInt16 wSuiteMask;
    public byte wProductType;
    public byte wReserved;
}

You will need this using statement:

    using System.Runtime.InteropServices;

At the top of your relevant class, declare:

    [DllImport("kernel32", EntryPoint = "GetVersionEx")]
    static extern bool GetVersionEx(ref OsVersionInfoEx osVersionInfoEx);

Now call the code as follows:

        const int VER_NT_WORKSTATION = 1;
        var osInfoEx = new OsVersionInfoEx();
        osInfoEx.dwOSVersionInfoSize = Marshal.SizeOf(osInfoEx);
        try
        {
            if (!GetVersionEx(ref osInfoEx))
            {
                throw(new Exception("Could not determine OS Version"));

            }
            if (osInfoEx.dwMajorVersion == 6 && osInfoEx.dwMinorVersion == 2 
                && osInfoEx.wProductType == VER_NT_WORKSTATION)
                MessageBox.Show("You've Got windows 8");

        }
        catch (Exception)
        {

            throw;
        }
Bruno
  • 498
  • 1
  • 4
  • 9
-1

Not sure if this is correct as I can only check on the version of windows 8 I have.

 int major = Environment.OSVersion.Version.Major;
 int minor = Environment.OSVersion.Version.Minor;

if ((major >= 6) && (minor >= 2))
{
    //do work here
}
Tsukasa
  • 6,342
  • 16
  • 64
  • 96
  • 8
    What will happen for major = 7 and minor = 0? – GvS Apr 16 '13 at 13:34
  • Better to just get the version and substring(Environment.OSVersion.Version.ToString().Substring(0,3)), then compare to the version table(http://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx). – Random IT Guy Apr 16 '13 at 14:07
  • You can change the operators as needed but from what I posted it will accept anything above windows 8 / server 2012. As you can see from the MSDN link posted above they are currently only 6.2.x.x – Tsukasa Apr 16 '13 at 14:37
  • 3
    Why not use `Version.CompareTo`? `if( Environment.OSVersion.Version.CompareTo( new Version(6, 2) ) > 0 ) { /* win8 or later */ }` – Dai Jul 22 '13 at 18:10
  • 1
    Seems like a hybrid of Asik's and @AngentFire's solutions is what is needed. No point in trying to re-invent the >= operator that the Version class already implements. Always surprising how simple it is to get things like that wrong when you try to over-optimize. – BTJ Jul 22 '13 at 20:18
  • This answer is very wrong. The number of upvotes is truly remarkable. – Andreas Rejbrand Aug 22 '18 at 19:43