27
votes

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

4
I send it to a WebService and there I sort the Requests by OS-Version. - SwissPrime
I cannot see anything obvious in the API documentation, so you could, perhaps, hardcode it in your Metro app version to begin with? - Tomas McGuinness
There is intentionally no way of getting the OS version. Historically applications have mis-used the OS version instead of relying on various forms of feature detection which have caused significant appcompat issues for the development team. For Windows 8 the dev team decided to avoid the issue entirely by not providing a GetVersion API. - ReinstateMonica Larry Osterman
@LarryOsterman - can you show an example of how to do feature detection for c#/xaml apps? will we have to use reflection? - Robert Levy
Right now there is no language projection support for feature detection in C#/Xaml, because there is only one version (and thus all features are available). The WinRT platform has support for versioning and feature detection, but the language projections have not implemented it (because there is no way of testing their implementation). I can't even speculate on how feature detection would be implemented because I'm not involved in the design of those features. - ReinstateMonica Larry Osterman

4 Answers

11
votes

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.

6
votes

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/

5
votes

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;
    }
0
votes

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