Recently I need to write some codes to disable one feature on Windows 7 for an application. For Windows 7, its major number is 6 and minor number is 1 (see this link http://msdn.microsoft.com/en-us/library/ms724832(VS.85).aspx) and thus we can compare the version number to detect that. However, that is not enough. Because Windows Server 2008 R2 also use this version number, we have to filter it further by using product type. In our managed code, System.OperatingSystem can retrieve version number easily, but there is no available API for retrieving product type, so we have to wrap native function GetVersionEx(). Something like this:
if (osInfo.Platform == System.PlatformID.Win32NT && osInfo.Version.Major == 6 && osInfo.Version.Minor == 1)
{
if (SafeNativeMethods.GetVersionEx(versionInfo))
if (versionInfo.wProductType == ProductType.Windows_Workstation)
// This is Windows 7
}
[DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal extern static bool GetVersionEx([In, Out] OSVERSIONINFO pVersionInfo);
http://msdn.microsoft.com/en-us/library/ms724832.aspx
It points out at least two points:
Obviously, GetVersionEx() is not suitable here. VerifyVersionInfo() should be the friend. The following link gives very good example in C++ to show how to detect if the OS is Windows XP SP2 or later.
http://msdn.microsoft.com/en-us/library/ms725491(VS.85).aspx
Following the example in above link, we can wrap VerifyVersionInfo() in managed code to detect if the running OS is Windows 7 or later.