2016-11-18 376 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的信息...

相关问题