在 Windows 10 C# UWP 通用 Windows 应用程序中获取用户名 [英] Get UserName in a Windows 10 C# UWP Universal Windows app
本文介绍了在 Windows 10 C# UWP 通用 Windows 应用程序中获取用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力处理 Windows 10 UWP 世界中的另一个简单任务.
I am struggling with yet another simple task in the Windows 10 UWP world.
我只需要当前 Windows 用户的用户名.Environment.UserName 在 UWP 中不是一个东西.到目前为止,在网络上搜索没有任何帮助.因此我的帖子在这里.
I simply need the UserName of the current Windows user. Environment.UserName is just not a thing in UWP. And no amount of searching the web has helped so far. Hence my post here.
有人吗?现在这不可能吗?
Anyone? Is this just not possible now?
推荐答案
- 在 Package.appxmanifest 中为您的应用添加用户帐户信息"功能
使用此代码获取用户显示名称:
Use this code to get user display name:
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
IReadOnlyList<User> users = await User.FindAllAsync();
var current = users.Where(p => p.AuthenticationStatus == UserAuthenticationStatus.LocallyAuthenticated &&
p.Type == UserType.LocalUser).FirstOrDefault();
// user may have username
var data = await current.GetPropertyAsync(KnownUserProperties.AccountName);
string displayName = (string)data;
//or may be authinticated using hotmail
if(String.IsNullOrEmpty(displayName))
{
string a = (string)await current.GetPropertyAsync(KnownUserProperties.FirstName);
string b = (string)await current.GetPropertyAsync(KnownUserProperties.LastName);
displayName = string.Format("{0} {1}", a, b);
}
text1.Text = displayName;
}
这篇关于在 Windows 10 C# UWP 通用 Windows 应用程序中获取用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文