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.
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.
Version win8version = new Version(6, 2, 9200, 0);
if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
Environment.OSVersion.Version >= win8version)
{
// its win8 or 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.
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));
}
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";
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;
}
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
}