获取用户名在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?
感谢。
推荐答案
1
- 添加用户帐户信息功能,为您的应用
-
使用此代码获取用户的显示名称:
- Add "User Account Information" capability to your app
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屋!
查看全文