Get OS-Version in WinRT Metro App C#

12,242

Solution 1

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/

Solution 2

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.

Solution 3

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;
    }
Share:
12,242

Related videos on Youtube

SwissPrime
Author by

SwissPrime

Updated on July 02, 2022

Comments

  • SwissPrime
    SwissPrime almost 2 years

    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

    • SwissPrime
      SwissPrime about 12 years
      I send it to a WebService and there I sort the Requests by OS-Version.
    • Tomas McGuinness
      Tomas McGuinness about 12 years
      I cannot see anything obvious in the API documentation, so you could, perhaps, hardcode it in your Metro app version to begin with?
    • Larry Osterman
      Larry Osterman about 12 years
      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.
    • Robert Levy
      Robert Levy about 12 years
      @LarryOsterman - can you show an example of how to do feature detection for c#/xaml apps? will we have to use reflection?
    • Larry Osterman
      Larry Osterman about 12 years
      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.
    • Larry Osterman
      Larry Osterman about 12 years
      Winrt platform versioning works as follows: Every winrt type has the version in which it was introduced. Except for runtime classes and enums, winrt types are immutable. Runtime classes and enums can be additively versioned, the new entries indicate the version in which they were introduced. Language projections can use this information to figure out which APIs and values are available on which OS version. How they do this is up to the language projection. Martyn's talk here: channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2012/… has more information on versioning.
  • svick
    svick about 12 years
    I suppose it's future-proofing. Metro-style apps will most likely run on Windows 9 as well.
  • Robert Levy
    Robert Levy about 12 years
    @svick - What's he gonna do? Show an error message if run on future OS's? That's not future proofing.
  • svick
    svick about 12 years
    @RobertLevy, as the OP said in a comment, send the OS version to a WebService.
  • linkerro
    linkerro about 12 years
    Metro apps are meant to run in a silo and have 0 knowledge of the underlying operating system. Also updates to WinRT may come out of band. Also, you should check out other metrics that are more important then the windows kernel version (screen size, location and so on). The next os from MS might not even be a windows for all we know.
  • DamienG
    DamienG almost 11 years
    I assure you it does, I'm using it in CSharpAnalytics and am seeing OS version for thousands for installs.
  • Jerry Nixon
    Jerry Nixon almost 11 years
    You should post your version, then. That version didn't work for me. But I make mistakes. Sorry.
  • DamienG
    DamienG almost 11 years
    I just copied the code directly from the blog into a brand new Windows Store project and called each method from MainPage.xaml.cs. The watch Window shows it is working fine. If you're still not seeing that let me know what is happening so we can figure out if it is something machine specific or not.
  • DamienG
    DamienG almost 11 years
    Additionally this person is asking for OS version information, you linked to an answer on CPU architecture.
  • Gabriel S.
    Gabriel S. almost 11 years
    How can you anticipate just like that that there "is no reason" to check for OS version? What if, for statistics purposes, it actually is relevant to get the exact OS version the app is running on (it may not stay fixed at 6.2.9200.0 forever, don't you think?) ? I'm sorry, but this is not an actual answer, it's just a patronizing way to avoid it.
  • DamienG
    DamienG almost 11 years
    Also just validated on Windows 8.1 - is correctly returning 6.3 as OS version number. (Windows 8 is kernel 6.2)
  • Ian Griffiths
    Ian Griffiths almost 11 years
    It can be very useful for support purposes to know which version of the OS someone is running. If you provide a way to send an email to the support dept from directly within the app, it's useful to embed the OS version details in that email.
  • linkerro
    linkerro almost 11 years
    I admit that now that win 8.1 is here I should take a look and see if the status quo has changed.
  • Ian Griffiths
    Ian Griffiths almost 11 years
    One tricky thing with this HAL approach is that some machines actually report multiple HAL drivers. A Surface RT will report no fewer than four! Two of them have the same version number as Windows, but two (both from NVidia) have a version number starting with a 9.
  • borrrden
    borrrden almost 11 years
    @JerryNixon-MSFT This works as advertised for me as well, and the link you gave is for determining the type of processor. We need this code to determine whether or not to use WebView or WebViewBrush since the need for WebViewBrush is gone in 8.1 (and in some cases it completely goes nuts when the app bar is opened). Do you have a solution or is this one the best we can hope for?
  • Quincy
    Quincy over 9 years
    This will only tell you whether its WP8.1. It won't tell you what the OS build number is e.g. whether phone is on dev preview or not. This is very important for me to know what OS build the phone is on. Bugs exist in some builds and not in other builds, I'd like to make an error report stating the OS build number
  • DamienG
    DamienG over 9 years
    Fixed the link Quincy. The code has been subsequently updated to not rely on the HAL but instead uses the most likely version based on all Microsoft supplied drivers.
  • Cyprien Autexier
    Cyprien Autexier over 9 years
    Yes this does not work with windows phone :), but the question was about windows 8. I'm afraid that unless Microsoft allows us to use version helper apis we won't have any better option :(.
  • Quincy
    Quincy over 9 years
    Good to know this should work for tablet. The question at the time was for win8 , but now WP/W8.1 have universal projects that share all code
  • palota
    palota about 9 years
    It does not work. It always returns Windows NT 6.2, but I have Windows 10.
  • Cyprien Autexier
    Cyprien Autexier about 9 years
    I have not tested it on windows 10, but I believe you. the new kernel should be name Windows NT 10 but since it is still preview it might not have found its way to the User Agent. Let's hope there will be an alternative in the news apis ! Maybe an access to environment variables? Who knows.
  • Cyprien Autexier
    Cyprien Autexier about 9 years
    You should take at the solution below which looks like a better way to do this now it's updated.
  • Sevenate
    Sevenate almost 9 years
    @DamienG, thanks for the code sample - it works great! The only question I have is - what about x86? It looks like you only track x64 or AMD architecture in GetSystemUserAgentAsync() and I'm not sure that I could pick appropriate member from ProcessorArchitecture enum for x86. Any advice?
  • DamienG
    DamienG almost 9 years
    Use GetProcessorArchitecture in that sample to detect CPU directly. The GetSystemUserAgent doesn't format for x86 as browsers don't either - the absence of x64 or ARM equals x86 in user agent terms.
  • YasharBahman
    YasharBahman almost 9 years
    @GabrielS. I logged in just to upvote you. The "you shouldn't be doing this" response is not an answer.
  • Hong
    Hong almost 8 years
    The should be THE answer. Thank you!