Adam Benoit

Windows Phone Development

Getting system info on Windows Phone

Posted on

by Adam

Today I want to show how to access various properties available to developers on Windows Phone. I needed some of these and made a quick demo app.

After looking around the net, I found the following resources:

Download ABSystemInfo Solution

First, a Pivot based view page:

<phone:PhoneApplicationPage 
    x:Class="ABSystemInfo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait"  Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <!--Pivot Control-->
        <controls:Pivot Title="DEVICE INFO">
            <controls:PivotItem Header="device">
                <Grid>
                    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Height="607" Width="456">
                        <TextBlock Text="Device Manufacturer" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="DeviceManufacturerTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="Device Name" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="DeviceNameTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="Device Firmware Version" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="DeviceFirmwareVersionTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="Device Hardware Version" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="DeviceHardwareVersionTextBlock" FontSize="36" TextAlignment="Center"/>
                        </StackPanel>

                </Grid>
            </controls:PivotItem>

            <controls:PivotItem Header="memory">
                <Grid>
                    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Height="607" Width="456">
                        <TextBlock Text="Application Current Memory Usage" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="ApplicationCurrentMemoryUsageTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="Application Memory Usage Limit" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="ApplicationMemoryUsageLimitTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="Application Peak Memory Usage" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="ApplicationPeakMemoryUsageTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="DeviceTotalMemory" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="DeviceTotalMemoryTextBlock" FontSize="36" TextAlignment="Center"/>

                    </StackPanel>
                </Grid>
            </controls:PivotItem>

            <controls:PivotItem Header="hardware">
                <Grid>
                    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Height="607" Width="456">
                        <TextBlock Text="Is Keyboard Deployed?" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="IsKeyboardDeployedTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="Is Keyboard Present?" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="IsKeyboardPresentTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="Power Source" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="PowerSourceTextBlock" FontSize="36" TextAlignment="Center"/>
                    </StackPanel>
                </Grid>
            </controls:PivotItem>

            <controls:PivotItem Header="os">
                <Grid>
                    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Height="607" Width="456">
                        <TextBlock Text="Operating System" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="OperatingSystemPlatformTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="Version" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="OperatingSystemVersionTextBlock" FontSize="36" TextAlignment="Center"/>
                    </StackPanel>
                </Grid>
            </controls:PivotItem>

            <controls:PivotItem Header="clr">
                <Grid>
                    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Height="607" Width="456">
                        <TextBlock Text="Major" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="ClrMajorTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="Minor" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="ClrMinorTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="Revision" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="ClrRevisionTextBlock" FontSize="36" TextAlignment="Center"/>
                        <TextBlock Text="Build" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="ClrBuildTextBlock" FontSize="36" TextAlignment="Center"/>
                    </StackPanel>
                </Grid>
            </controls:PivotItem>

            <controls:PivotItem Header="unique ids">
                <Grid>
                    <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top" Height="607" Width="456">
                        <TextBlock Text="Device Unique ID" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="DeviceUniqueIDTextBlock" FontSize="36" TextAlignment="Center" TextWrapping="Wrap"/>
                        <TextBlock Text="Windows Live Anonymous ID" Margin="10" Foreground="{StaticResource PhoneAccentBrush}" FontSize="24" TextAlignment="Center"/>
                        <TextBlock Text="0" Margin="10" Name="WindowsLiveAnonymousIDTextBlock" FontSize="36" TextAlignment="Center" TextWrapping="Wrap"/>
                    </StackPanel>
                </Grid>
            </controls:PivotItem>
        </controls:Pivot>
    </Grid>
</phone:PhoneApplicationPage>

And finally the code behind file:

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Threading;
using Microsoft.Phone.Info;

namespace ABSystemInfo
{
    public partial class MainPage
    {
        readonly DispatcherTimer _timer;

        private long _applicationCurrentMemoryUsage;
        private long _applicationMemoryUsageLimit;
        private long _applicationPeakMemoryUsage;
        private long _deviceTotalMemory;

        private const int AnidLength = 32;
        private const int AnidOffset = 2;

        public MainPage()
        {
            InitializeComponent();
            LoadStaticInfo();
            _timer = new DispatcherTimer();
            _timer.Interval = new TimeSpan(0, 0, 1);
            _timer.Tick += TimerTick;
            _timer.Start();

        }

        void TimerTick(object sender, EventArgs e)
        {
            try
            {
                _applicationCurrentMemoryUsage = DeviceStatus.ApplicationCurrentMemoryUsage;
                _applicationMemoryUsageLimit = DeviceStatus.ApplicationMemoryUsageLimit;
                _applicationPeakMemoryUsage = DeviceStatus.ApplicationPeakMemoryUsage;

                _deviceTotalMemory = DeviceStatus.DeviceTotalMemory;

                ApplicationCurrentMemoryUsageTextBlock.Text = String.Format("{0} MB ({1} KB)", ((_applicationCurrentMemoryUsage / 1024) / 1024), (_applicationCurrentMemoryUsage / 1024));
                ApplicationMemoryUsageLimitTextBlock.Text = String.Format("{0} MB ({1} KB)", ((_applicationMemoryUsageLimit / 1024) / 1024), (_applicationMemoryUsageLimit / 1024));
                ApplicationPeakMemoryUsageTextBlock.Text = String.Format("{0} MB ({1} KB)", ((_applicationPeakMemoryUsage / 1024) / 1024), (_applicationPeakMemoryUsage / 1024));
                DeviceTotalMemoryTextBlock.Text = String.Format("{0} MB ({1} KB)", ((_deviceTotalMemory / 1024) / 1024), (_deviceTotalMemory / 1024));

                IsKeyboardDeployedTextBlock.Text = DeviceStatus.IsKeyboardDeployed.ToString(CultureInfo.InvariantCulture);
                IsKeyboardPresentTextBlock.Text = DeviceStatus.IsKeyboardPresent.ToString(CultureInfo.InvariantCulture);
                PowerSourceTextBlock.Text = DeviceStatus.PowerSource.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void LoadStaticInfo()
        {
            DeviceFirmwareVersionTextBlock.Text = DeviceStatus.DeviceFirmwareVersion;
            DeviceHardwareVersionTextBlock.Text = DeviceStatus.DeviceHardwareVersion;
            DeviceManufacturerTextBlock.Text = DeviceStatus.DeviceManufacturer;
            DeviceNameTextBlock.Text = DeviceStatus.DeviceName;

            OperatingSystemPlatformTextBlock.Text = Environment.OSVersion.Platform.ToString();
            OperatingSystemVersionTextBlock.Text = Environment.OSVersion.Version.ToString();

            ClrBuildTextBlock.Text = Environment.Version.Build.ToString(CultureInfo.InvariantCulture);
            ClrMajorTextBlock.Text = Environment.Version.Major.ToString(CultureInfo.InvariantCulture);
            ClrMinorTextBlock.Text = Environment.Version.Minor.ToString(CultureInfo.InvariantCulture);
            ClrRevisionTextBlock.Text = Environment.Version.Revision.ToString(CultureInfo.InvariantCulture);

            string deviceUniqueId = String.Empty;
            for (int i = 0; i < GetDeviceUniqueId().GetLength(0); i++)
            {
                deviceUniqueId += GetDeviceUniqueId().GetValue(i);
            }

            DeviceUniqueIDTextBlock.Text = deviceUniqueId;
            WindowsLiveAnonymousIDTextBlock.Text =
                GetWindowsLiveAnonymousId().ToString(CultureInfo.InvariantCulture);
        }

        //Note: to get a result requires ID_CAP_IDENTITY_DEVICE
        // to be added to the capabilities of the WMAppManifest
        // this will then warn users in marketplace

        public static byte[] GetDeviceUniqueId()
        {
            byte[] result = null;
            object uniqueId;
            if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId))
                result = (byte[])uniqueId;

            return result;
        }

        // NOTE: to get a result requires ID_CAP_IDENTITY_USER
        //  to be added to the capabilities of the WMAppManifest
        // this will then warn users in marketplace

        public static string GetWindowsLiveAnonymousId()
        {
            string result = String.Empty;
            object anid;
            if (UserExtendedProperties.TryGetValue("ANID", out anid))
            {
                if (anid != null && anid.ToString().Length >= (AnidLength + AnidOffset))
                {
                    result = anid.ToString().Substring(AnidOffset, AnidLength);
                }
            }

            return result;
        }
    }
}

 

About Adam

Adam Benoit is a Quality Assurance Analyst, Contributing Author and Windows Phone Developer. View all posts by Adam →
This entry was posted in Applications, Development, Featured, Howto. Bookmark the permalink.

Leave a Reply