System.Environment.OSVersion返回错误的版本

pge*_*e70 22 c#

使用Windows 10,从Windows 8升级=> 8.1 => 10当我使用此代码时.

OperatingSystem os = System.Environment.OSVersion;
Run Code Online (Sandbox Code Playgroud)

os.Version = {6.2.9200.0} System.Version

我读到这是因为它显示的版本,但我不明白这意味着什么.

我想要正确的操作系统版本,因为我在Web服务上记录用户代理字符串,并且想要正确识别Windows版本以获得支持.什么是最简单的方法来正确报告正确的版本?

Nas*_*ine 29

除非您使用清单声明应用程序兼容,否则Windows 10将返回该字符串.为此,添加app.manifest(右键单击您的项目 - >添加 - >新项目 - >应用程序清单文件),然后取消注释以下行:

<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />
Run Code Online (Sandbox Code Playgroud)

您可以对Windows Vista到Windows 10执行相同的操作.所有内容都在同一部分中:

<application>
  <!-- A list of the Windows versions that this application has been tested on and is
       is designed to work with. Uncomment the appropriate elements and Windows will 
       automatically selected the most compatible environment. -->

  <!-- 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}" />-->

  <!-- Windows 8.1 -->
  <!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->

  <!-- Windows 10 -->
  <supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />

</application>
Run Code Online (Sandbox Code Playgroud)

现在,当您运行应用程序时,它将报告正确的10.0.*.0版本

  • @ pgee70没有更多内容了.我尝试了我的结果,它在发布和调试中都有效.只需确保它是您正在部署的正确的exe,并在应用程序选项卡的项目配置中包含正确的清单,如您所见[http://l.imgur.com/WxuZ8KW.png?1 ) (2认同)