ARTICLE

User Information in Windows 8 App

Posted by Mahesh Chand Articles | Windows Store Apps January 08, 2013
This article demonstrates how to get and set a user's profile information within a Windows Store app.
Reader Level:

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. 

Login to add your contents and source code to this article
post comment
     

This is great!

Posted by Ibrahim Ersoy Jan 08, 2013
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.