2016-11-18 80 views
1

我使用C#。我試圖讓操作系統的最新版本:獲取C#中Windows 10的最新版本操作系統#

OperatingSystem os = Environment.OSVersion; 
Version ver = os.Version; 

我上的Windows 106.2

6.2的Windows 8的WindowsServer 2012Detect Windows version in .net

我發現下面的解決方案(How can I detect if my app is running on Windows 10)。

static bool IsWindows10() 
{ 
    var reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"); 
    string productName = (string)reg.GetValue("ProductName"); 
    return productName.StartsWith("Windows 10"); 
} 

這是最好的方式獲得在C#中的當前版本?

+1

也許檢查在這一個:http://stackoverflow.com/questions/6331826/get-os-version-friendly-name-in-c-sharp –

+1

@olga你添加了清單+ supportedOS指導? – magicandre1981

回答

3

添加application manifest到您的應用程序,並添加Windows 8.1和Windows 10的supportedOS Id到清單:

<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}"/> 
     </application> 
    </compatibility> 

現在Environment.OSVersion包括正確的數據對於Windows 8.1和Windows 10而不是6.2來表示您運行Windows 8.這是一個change since Windows 8.1

+0

謝謝,它的作品! – Olga

0

這是Microsoft官方鏈接,指示how to get the System Version。這實際上是對Version API Helper Functions

通話所以基本上必須將此代碼轉換成C#,因爲它是在C++中,然後只保留在Windows 10的一部分...

#include <windows.h> 
#include <stdio.h> 
#include <VersionHelpers.h> 

int 
__cdecl 
wmain(
    __in int argc, 
    __in_ecount(argc) PCWSTR argv[] 
    ) 
{ 
    UNREFERENCED_PARAMETER(argc); 
    UNREFERENCED_PARAMETER(argv); 
    if (IsWindows10OrGreater()) 
    { 
     printf("Windows10OrGreater\n"); 
    } 
} 

,如果你喜歡嘗試閱讀代碼,你可以看看this one link。此DLL可以用來獲取OS的信息...

相關問題