Google Windows Store Apps: Get OS Version, beginners tutorials (C#-XAML) | SubramanyamRaju Xamarin & Windows App Dev Tutorials

Monday 4 January 2016

Windows Store Apps: Get OS Version, beginners tutorials (C#-XAML)

Introduction:

It is time to write an article about "How to get OS Version in Winrt platfrom?", previously in silverlight platform(WP8.0/WP8.1) we can easily get windows phone device OS version with 'Environment.OSVersion', unfortunately now in winrt platfrom 'Environment' class doesn't have property like 'OSVersion'. However we have an alternative way, and we can learn it from this article :)

Requirements:

  • This sample is targeted for windowsphone winrt 8.1 OS,So make sure you’ve downloaded and installed the Windows Phone 8.1 SDK. For more information, see Get the SDK.
  • I assumes that you’re going to test your app on the Windows Phone emulator. If you want to test your app on a phone, you have to take some additional steps. For more info, see Register your Windows Phone device for development.
  • This post assumes you’re using Microsoft Visual Studio Express 2013 for Windows or Later.

Description:

Previously in Silverlight platfrom(WP8.0/WP8.1), we are using below code to get OS version.

Silverlight WindowsPhone:
  1. string OSVersion = Environment.OSVersion.Version.ToString();  

WinRT WindowsPhone:
Now lets start to write code for getting Windows Phone device OS Version in winrt.
Step 1:
1. Open Microsoft Visual Studio Express 2013 for Windows (or) later.
2. Create new windows phone winrt project using the "Blank App" template available under Visual C# -> Store Apps -> Windows Phone Apps. (for example project name :GetWindowsOS) 
And lets add below code in MainPage.xaml, which is having one button is for getting OS version, and one textblock is for displaying OS version.
  1. <Grid Background="White">  
  2.         <Grid Margin="5" VerticalAlignment="Center">  
  3.             <Grid.RowDefinitions>  
  4.                 <RowDefinition Height="Auto"/>  
  5.                 <RowDefinition Height="Auto"/>  
  6.             </Grid.RowDefinitions>  
  7.             <Button Name="BtnGetOS" Content="Get os version" HorizontalAlignment="Stretch" Background="#FF34ACB8" Click="BtnGetOS_Click"/>  
  8.             <TextBlock Grid.Row="1" Name="TbckOSVersion" Text="Current Windows Device Version: 10.0.10586.11" FontWeight="Bold" FontSize="17" Foreground="Gray"/>  
  9.         </Grid>  
  10.     </Grid>  

And in MainPage.xaml.cs , add namespace 'using System.Reflection;' and  write below code on button click event.
  1. private void BtnGetOS_Click(object sender, RoutedEventArgs e)  
  2.         {  
  3.             GetWindowsOS_WINRT();  
  4.         }  

  5.         public void GetWindowsOS_WINRT()  
  6.         {  
  7.             var analyticsInfoType = Type.GetType("Windows.System.Profile.AnalyticsInfo, Windows, ContentType=WindowsRuntime");  
  8.             var versionInfoType = Type.GetType("Windows.System.Profile.AnalyticsVersionInfo, Windows, ContentType=WindowsRuntime");  
  9.   
  10.             if (analyticsInfoType == null || versionInfoType == null)  
  11.             {  
  12.                 TbckOSVersion.Text="Apparently you are not on Windows 10";  
  13.                 return;  
  14.             }  
  15.   
  16.             var versionInfoProperty = analyticsInfoType.GetRuntimeProperty("VersionInfo");  
  17.   
  18.             object versionInfo = versionInfoProperty.GetValue(null);  
  19.             var versionProperty = versionInfoType.GetRuntimeProperty("DeviceFamilyVersion");  
  20.             object familyVersion = versionProperty.GetValue(versionInfo);  
  21.             long versionBytes;  
  22.             if (!long.TryParse(familyVersion.ToString(), out versionBytes))  
  23.             {  
  24.                 TbckOSVersion.Text="Can't parse version number";  
  25.                 return;  
  26.             }  
  27.   
  28.             Version DeviceVersion= new Version((ushort)(versionBytes >> 48),  
  29.               (ushort)(versionBytes >> 32),  
  30.               (ushort)(versionBytes >> 16),  
  31.               (ushort)(versionBytes));  
  32.   
  33.            TbckOSVersion.Text="Current Windows Device Version: " + DeviceVersion;  
  34.         }  

Output:

Important Notes:
1. Don't forgot to add namespace 'using System.Reflection;
2. This sample will be only work on windows 10 devices. and it will return OS version for only windows 10 devices and it can't work for windows phone 8.0/8.1devices
GetWindowsOSVersion
FeedBack Note:
Please share your thoughts,what you think about this post,Is this post really helpful for you?I always welcome if you drop comments on this post and it would be impressive.

Follow me always at  
Have a nice day by  :)

2 comments:

  1. hi.. how to get the OS version in Windows phone 8.1. I have tried Environment.OSVersion, but it is not working. Please help me on this.

    ReplyDelete
  2. Thank u so very much for posting such a wonderful Information.
    free cricket games | World Cricket Championship 2 APK

    ReplyDelete

Search Engine Submission - AddMe