我們都知道在C#中可以通過Environment.OSVersion來判斷當前操作系統,下面是操作系統和主次版本的對應關系:
操作系統 | 主版本.次版本 |
Windows 10 | 10.0* |
Windows Server 2016 Technical Preview | 10.0* |
Windows 8.1 | 6.3* |
Windows Server 2012 R2 | 6.3* |
Windows 8 | 6.2 |
Windows Server 2012 | 6.2 |
Windows 7 | 6.1 |
Windows Server 2008 R2 | 6.1 |
Windows Server 2008 | 6 |
Windows Vista | 6 |
Windows Server 2003 R2 | 5.2 |
Windows Server 2003 | 5.2 |
Windows XP 64-Bit Edition | 5.2 |
Windows XP | 5.1 |
Windows 2000 | 5 |
圖標轉自(https://msdn.microsoft.com/en-us/library/ms724832(v=vs.85).aspx)
但是,當你在win10操作系統上使用這個函數是會得到這樣的結果:6.2.9200.0,而不是我們期待的10.0.***,這是為什么?值得我們注意的是,在這個表格下面有這樣的小字:
* For applications that have been manifested for Windows 8.1 or Windows 10. Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2). To manifest your applications for Windows 8.1 or Windows 10, refer to Targeting your application for Windows.
也就是說返回6.2就是win8以上操作系統的默認情況。要返回10.0就要做特殊處理。MSDN原文方法在這里https://msdn.microsoft.com/en-us/library/dn481241(v=vs.85).aspx
解決方法:為項目新建並指定“應用程序清單文件”,然后把下面的注釋去掉,就可以返回10.0.***了