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:
- 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.
- <Grid Background="White">
- <Grid Margin="5" VerticalAlignment="Center">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="Auto"/>
- </Grid.RowDefinitions>
- <Button Name="BtnGetOS" Content="Get os version" HorizontalAlignment="Stretch" Background="#FF34ACB8" Click="BtnGetOS_Click"/>
- <TextBlock Grid.Row="1" Name="TbckOSVersion" Text="Current Windows Device Version: 10.0.10586.11" FontWeight="Bold" FontSize="17" Foreground="Gray"/>
- </Grid>
- </Grid>
And in MainPage.xaml.cs , add namespace 'using System.Reflection;' and write below code on button click event.
- private void BtnGetOS_Click(object sender, RoutedEventArgs e)
- {
- GetWindowsOS_WINRT();
- }
- public void GetWindowsOS_WINRT()
- {
- var analyticsInfoType = Type.GetType("Windows.System.Profile.AnalyticsInfo, Windows, ContentType=WindowsRuntime");
- var versionInfoType = Type.GetType("Windows.System.Profile.AnalyticsVersionInfo, Windows, ContentType=WindowsRuntime");
- if (analyticsInfoType == null || versionInfoType == null)
- {
- TbckOSVersion.Text="Apparently you are not on Windows 10";
- return;
- }
- var versionInfoProperty = analyticsInfoType.GetRuntimeProperty("VersionInfo");
- object versionInfo = versionInfoProperty.GetValue(null);
- var versionProperty = versionInfoType.GetRuntimeProperty("DeviceFamilyVersion");
- object familyVersion = versionProperty.GetValue(versionInfo);
- long versionBytes;
- if (!long.TryParse(familyVersion.ToString(), out versionBytes))
- {
- TbckOSVersion.Text="Can't parse version number";
- return;
- }
- Version DeviceVersion= new Version((ushort)(versionBytes >> 48),
- (ushort)(versionBytes >> 32),
- (ushort)(versionBytes >> 16),
- (ushort)(versionBytes));
- TbckOSVersion.Text="Current Windows Device Version: " + DeviceVersion;
- }
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
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 @Subramanyam_B
Have a nice day by Subramanyam Raju :)
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.
ReplyDeleteThank u so very much for posting such a wonderful Information.
ReplyDeletefree cricket games | World Cricket Championship 2 APK