如何获得登录用户名PC在Windows应用商店的应用程序? [英] How to get logged in PC username in Windows Store Apps?

查看:136
本文介绍了如何获得登录用户名PC在Windows应用商店的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想在这个线程我如何使用C#得到.NET当前用户名?

但代码

string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;



似乎并没有在Windows 8的应用程序工作。

doesn't seem to work on Windows 8 apps.

我怎么能做到这一点。

推荐答案

步骤1:

创建一个使用C#和XAML语言空白Windows应用商店的应用程序

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

步骤2:


Step 2:

    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>

步骤3:

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; 

步骤4:

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.

请注意:这是仅适用于Microsoft帐户aviablable

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);              

}



图片和源代码:

这篇关于如何获得登录用户名PC在Windows应用商店的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆