User Information in Windows 8 App

Windows 8 provides an API to get and set a user's account information including from a Windows 8 online account as well as local account. For example, we can get and set a user's display name or profile picture from code using this API. The UserInformation class is used for this purpose.  

UserInformation Class

The UserInformation class provides static methods to get and set user profile settings including user name and account picture.

User Name

The GetDisplayNameAsync method gets the display name for the user account.

The GetFirstNameAsync method gets the user's first name.

The GetLastNameAsync method gets the user's last name.

The GetPrincipalNameAsync method gets the principal name for the user.

The following code snippet demonstrates how to get a user's name.

// Get display name
string displayName = await Windows.System.UserProfile.UserInformation.GetDisplayNameAsync();
UserTextBox.Text = displayName; 

// Get first name, last name and domain  name
string firstName = await Windows.System.UserProfile.UserInformation.GetFirstNameAsync();
string lastName = await Windows.System.UserProfile.UserInformation.GetLastNameAsync();
string domainName = await Windows.System.UserProfile.UserInformation.GetDomainNameAsync();

Account Picture

The GetAccountPicture and SetAccountPictureAsync methods are used to get and set the user's account picture.

The following code snippet gets the user's picture and displays it in an Image control.

// Get account picture and display it in an Image control
StorageFile image = Windows.System.UserProfile.UserInformation.GetAccountPicture
    (Windows.System.UserProfile.AccountPictureKind.SmallImage) as StorageFile;
if (image != null)
{
    try
    {
        IRandomAccessStream imageStream = await image.OpenReadAsync();
        BitmapImage bitmapImage = new BitmapImage();
        bitmapImage.SetSource(imageStream);
        Image1.Source = bitmapImage;
        Image1.Visibility = Visibility.Visible; 

    }

    catch (Exception ex)

    {                  

    }
}

You need to make sure to import the following namespaces.

using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;

The UserInformation class also has a method to set a user account picture. Read the following article:

Set Account Picture in Windows Store app


Summary

In this article, we learned how to get and set the user name and account pictures. 

Up Next
    Ebook Download
    View all
    Learn
    View all