Answer
Use Environment.OSVersion
and add an application manifest file with relevant supportedOS
elements uncommented.
e.g. add this under <asmv1:assembly>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
</application>
</compatibility>
Reason
I don't like the answer from @Mitat Koyuncu because is uses the registry unnecessarily and as mentioned in the comments uses unreliable string parsing.
I also don't like the answer from @sstan because it uses third party code and it needs the application manifest anyway.
From MSDN:
Windows 10: VerifyVersionInfo returns false when called by
applications that do not have a compatibility manifest for Windows 8.1
or Windows 10 if the lpVersionInfo parameter is set so that it
specifies Windows 8.1 or Windows 10, even when the current operating
system version is Windows 8.1 or Windows 10. Specifically,
VerifyVersionInfo has the following behavior:
? If the application has no manifest, VerifyVersionInfo behaves as if the operation system version is Windows 8 (6.2).
? If the application has a manifest that contains the GUID that corresponds to Windows 8.1, VerifyVersionInfo behaves as if the operation system version is Windows 8.1 (6.3).
? If the application has a manifest that contains the GUID that corresponds
to Windows 10, VerifyVersionInfo behaves as if the operation system
version is Windows 10 (10.0).
The reason is because VerifyVersionInfo is deprecated in Windows 10.
I have tested on Windows 10 and indeed Environment.OSVersion
works exactly as expected when the app.Manifest contains the relevant GUID as above. That is most likely why they did not change or deprecate it from .Net Framework.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…