Retrieve User Information in Windows Store Apps Using C#

Today we are trying to get user account information in Windows Store Apps. User Account information consists of thier first name, last name, account picture etc. When the user logs into the device, this application will get his/her user account information.

To get user profile information I use the UserInformation Class. This class enables us to fetch all the requried information about the login user. This class has many Methods such as GetFirstName(), GetDisplayName() etc which we cover in the later section.

Now, we are going to create a simple Windows Store Application using C#. In this application we fetch all the requried user account information and dispaly it.

Step 1

Create a Blank Windows Store Application using C# and XAML language.

Step 2

In the MainPage.XAML page I design some textblocks and a button to dispaly user account information.

<Page

    x:Class="SetAccountPicture.UserProfileInformation"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:SetAccountPicture"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">

        <Grid Height="500" Width="1000">

            <Grid.RowDefinitions>

                <RowDefinition></RowDefinition>

                <RowDefinition></RowDefinition>

                <RowDefinition></RowDefinition>

                <RowDefinition></RowDefinition>

                <RowDefinition Height="auto"></RowDefinition>

            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>

                <ColumnDefinition></ColumnDefinition>

                <ColumnDefinition></ColumnDefinition>

            </Grid.ColumnDefinitions>

            <Button x:Name="Get_UserInformation" Click="Get_UserInformation_Click_1" Grid.Row="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Height="60" Width="250" Content="Get User Information" FontSize="20"></Button>

            <TextBlock Text="User's Display Name is: " Grid.Row="1" Grid.Column="0" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>

            <TextBlock Text="User First Name is: " Grid.Row="2" Grid.Column="0" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>

            <TextBlock Text="User Last Name is: " Grid.Row="3" Grid.Column="0" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>

            <TextBlock Text="Account Picture is: " Grid.Row="4" Grid.Column="0" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>

            <TextBlock x:Name="UserName" Text="User Name is: " Grid.Row="1" Grid.Column="1" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>

            <TextBlock x:Name="FistName" Text="First Name is: " Grid.Row="2" Grid.Column="1" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>

            <TextBlock x:Name="LastName" Text="Last Name is: " Grid.Row="3" Grid.Column="1" FontSize="25" VerticalAlignment="Center" HorizontalAlignment="Center"></TextBlock>

            <Image  x:Name="AccountPicture" Height="150" Width="200" Grid.Row="4" Grid.Column="2" Stretch="Fill"></Image>

        </Grid>

    </Grid>

</Page>

Step 3

In this step we write the C# code. First of all include the following namespaces in the .cs file:

using Windows.Storage;

using Windows.Storage.Pickers;

using Windows.Storage.Streams;

using Windows.System.UserProfile;

using Windows.UI.Core;

using Windows.UI.Xaml.Media.Imaging;


Step 4

Now, we are going to fetch user account related information using the UserInformaion Class.

Get the Display Name for the current User; see:

string displayName = await UserInformation.GetDisplayNameAsync();

if (string.IsNullOrEmpty(displayName))

{

     rootPage.NotifyUser("No Display Name was returned", NotifyType.StatusMessage);

}
else

{

    UserName.Text = displayName;

}

Get the first Name for the current User.

Note: This is only aviablable for Microsoft Accounts.

string firstName = await UserInformation.GetFirstNameAsync();

if (string.IsNullOrEmpty(firstName))
{

     rootPage.NotifyUser("No Display Name was returned", NotifyType.StatusMessage);

}

else

{

   FistName.Text = firstName;

}
 

Get the last Name for the current user; see:

string lastName = await UserInformation.GetLastNameAsync();

if (string.IsNullOrEmpty(lastName))

{

    rootPage.NotifyUser("No Display Name was returned", NotifyType.StatusMessage);

}

else

{

    LastName.Text = lastName;

}

Get the Account Picture for the current user.

Note: You can request three types of images. For Example: Small, large and video(dynamic image). If available it returns.

StorageFile image = UserInformation.GetAccountPicture(AccountPictureKind.SmallImage) as StorageFile;
           

if (image != null)

{

       rootPage.NotifyUser("SmallImage path = " + image.Path, NotifyType.StatusMessage);

     try

    {

         IRandomAccessStream imageStream = await image.OpenReadAsync();

         BitmapImage bitmapImage = new BitmapImage();

         bitmapImage.SetSource(imageStream);

         AccountPicture.Source = bitmapImage;                 

    }

    catch (Exception ex)

    {

         rootPage.NotifyUser("Error opening stream: " + ex.ToString(), NotifyType.ErrorMessage);

    }

 }
else

{

      rootPage.NotifyUser("Small Account Picture is not available", NotifyType.StatusMessage);             

}
 

In the preceding code I use the SmallImage type of image. AccountPictureKind is an Enum that gives us options to get the account picture in SmallImage, LargeImage and Video. You can use any of these as per your need.

Step 5

Now, build your application and run it. Click on the Get User Information button.

It will show you user account information on the screen.

User-Information-In-Windows-Store-Apps.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all