Get current version OS in Windows 10 in C#

99封情书 提交于 2019-12-04 04:04:21

问题


I Use C#. I try to get the current version of the OS:

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

I get on the Windows 10: 6.2.

But 6.2 is Windows 8 or WindowsServer 2012 (Detect Windows version in .net)

I found the following solution (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");
}

This is the best way to get the current version in C#?


回答1:


Add application manifest to your application and add the supportedOS Id of Windows 8.1 and Windows 10 to the manifest:

<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>

Now Environment.OSVersion includes the correct data for Windows 8.1 and Windows 10 and not 6.2 to indicate you run Windows 8. This is a change since Windows 8.1.




回答2:


Here is a link from Microsoft offical, indicating how to get the System Version. It actually is a call to the Version API Helper Functions

So basically you must convert this code into C# because it's in C++, then keep only the Windows 10 part...

#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");
    }
}

And if you like trying to read code, you can check out this one link. This DLL can be used to get information on the OS...



来源:https://stackoverflow.com/questions/40672109/get-current-version-os-in-windows-10-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!