27

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

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
SwissPrime
  • 443
  • 1
  • 6
  • 10
  • 1
    I send it to a WebService and there I sort the Requests by OS-Version. – SwissPrime Apr 12 '12 at 14:14
  • 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 Apr 12 '12 at 14:19
  • 5
    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. – Larry Osterman Apr 12 '12 at 14:56
  • @LarryOsterman - can you show an example of how to do feature detection for c#/xaml apps? will we have to use reflection? – Robert Levy Apr 12 '12 at 15:04
  • 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 Apr 12 '12 at 15:07
  • 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: http://channel9.msdn.com/Events/Lang-NEXT/Lang-NEXT-2012/The-Windows-Runtime has more information on versioning. – Larry Osterman Apr 12 '12 at 15:17

4 Answers4

11

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.

DamienG
  • 6,575
  • 27
  • 43
  • I assure you it does, I'm using it in CSharpAnalytics and am seeing OS version for thousands for installs. – DamienG Jun 24 '13 at 18:34
  • You should post your version, then. That version didn't work for me. But I make mistakes. Sorry. – Jerry Nixon Jun 24 '13 at 21:15
  • 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 Jun 25 '13 at 05:07
  • Additionally this person is asking for OS version information, you linked to an answer on CPU architecture. – DamienG Jun 25 '13 at 05:11
  • Also just validated on Windows 8.1 - is correctly returning 6.3 as OS version number. (Windows 8 is kernel 6.2) – DamienG Jun 27 '13 at 14:59
  • 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. – Ian Griffiths Jul 24 '13 at 13:04
  • @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? – borrrden Aug 09 '13 at 06:40
  • 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. – DamienG Jan 08 '15 at 00:12
  • @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? – Sevenate Jul 28 '15 at 07:11
  • 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. – DamienG Jul 28 '15 at 07:37
  • The should be THE answer. Thank you! – Hong Aug 02 '16 at 19:24
5

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/

linkerro
  • 5,318
  • 3
  • 25
  • 29
  • 3
    I suppose it's future-proofing. Metro-style apps will most likely run on Windows 9 as well. – svick Apr 12 '12 at 14:29
  • @svick - What's he gonna do? Show an error message if run on future OS's? That's not future proofing. – Robert Levy Apr 12 '12 at 14:47
  • 2
    @RobertLevy, as the OP said in a comment, send the OS version to a WebService. – svick Apr 12 '12 at 14:49
  • 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. – linkerro Apr 13 '12 at 07:58
  • 20
    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. – Gabriel S. Jun 26 '13 at 08:13
  • 5
    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. – Ian Griffiths Jul 23 '13 at 07:06
  • I admit that now that win 8.1 is here I should take a look and see if the status quo has changed. – linkerro Jul 23 '13 at 08:10
  • @GabrielS. I logged in just to upvote you. The "you shouldn't be doing this" response is not an answer. – YasharBahman Aug 13 '15 at 06:19
5

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;
    }
Cyprien Autexier
  • 1,948
  • 16
  • 20
  • 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 – Quincy Jan 07 '15 at 16:13
  • 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 :(. – Cyprien Autexier Jan 08 '15 at 01:03
  • 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 – Quincy Jan 08 '15 at 01:19
  • It does not work. It always returns Windows NT 6.2, but I have Windows 10. – palota Feb 27 '15 at 13:21
  • 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 Feb 27 '15 at 18:22
  • You should take at the solution below which looks like a better way to do this now it's updated. – Cyprien Autexier Feb 27 '15 at 18:29
0

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

Filip Skakun
  • 31,624
  • 6
  • 74
  • 100