问题描述:

I'm programming a Metro Style App with C# and the Visual Studio 11 Beta.

Now I want to get the OS-Version of the OS. How can I get this?

I found out how to do it in "normal" Applications.

There you take the Environment-Class with the attribute OSVersion but in .NET Core there isn't this attribute

网友答案:

For new applications you should check for specific features, not OS version.

As far as I can tell there is no reason to check for OS version as metro applications are only available for win 8.

Edit: Store applications are available on multiple Windows versions now but it is still recommended to test for features instead of OS versions. Minimum OS version is set as the build target for a project when you create it.

Edit #2: If you are interested in tracking OS versions in your application's install base you can integrate Application Insights into your project starting with applications targeted at Windows 8.1. Here's a howto on getting started with it:

http://azure.microsoft.com/en-us/documentation/articles/app-insights-windows-get-started/

网友答案:

You can get the OS version number with some risk that it might not be correct by using the devices API to get the driver version numbers for a low-level system components.

The accepted answer is correct in that you shouldn't tie functionality to the version number but there are valid reasons to use it such as analytics - it's useful to know when a lot of your users are already on a new version and that you should be considering an app update to take advantage of it.

https://github.com/AttackPattern/CSharpAnalytics/blob/master/Source/CSharpAnalytics/SystemInfo/WindowsStoreSystemInfo.cs has more information and sample code (disclosure, I wrote that code)

Note: The code has been updated and now handles custom/multiple HALs etc.

网友答案:

In fact there is a simple workaround to get a Windows Version string in its User Agent form (Windows NT 6.x).

To people wondering why we might want to do that : to gather statistics about our users Windows Version and make aware decisions about backward compatibility.

public class OsVersion
{
    public static Task<string> GetAsync()
    {
        var t = new TaskCompletionSource<string>();
        var w = new WebView();
        w.AllowedScriptNotifyUris = WebView.AnyScriptNotifyUri;
        w.NavigateToString("<html />");
        NotifyEventHandler h = null;
        h = (s, e) =>
        {
            try
            {
                var match = Regex.Match(e.Value, @"Windows\s+NT\s+\d+(\.\d+)?");
                if (match.Success)
                    t.SetResult(match.Value);
                else
                    t.SetResult("Unknowm");
            }
            catch (Exception ex) { t.SetException(ex); }
            finally { /* release */ w.ScriptNotify -= h; }
        };
        w.ScriptNotify += h;
        w.InvokeScript("execScript", new[] { "window.external.notify(navigator.appVersion); " });
        return t.Task;
    }
网友答案:

Based on the answers of other posters - there is no API to test for OS version, but there will be APIs to query for the version of a specific type. This means that you might not be able to tell the version of future releases of Windows with your current code, but if you keep updating your code - you could determine the OS version based on whether a specific type is available. For now you would assume to be using the current version of Windows 8.

Also, I have not tried, but it is possible that you could get it by doing interop with JavaScript and parsing "navigator.appVersion".

相关阅读:
Top