我正在努力解决Windows 10 UWP世界中的另一项简单任务。
我只需要当前Windows用户的UserName。 Environment.UserName在UWP中不是一个东西。到目前为止,搜索网络的数量都没有帮助。因此我的帖子在这里。
任何人?这现在不可能吗?
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;
}
我可以看到,有一个User
类(UWP):https://msdn.microsoft.com/en-us/library/windows/apps/windows.system.user.aspx
试试这个:
var users = await User.FindAllAsync(UserType.LocalUser);
var name = await users.FirstOrDefault().GetPropertyAsync(KnownUserProperties.AccountName);
您还可以从Application.OnLaunched方法中获取启动应用程序的用户,请参阅here。
您仍需要在清单中声明用户信息功能。
快速示例(省略号表示不适用的生成代码):
sealed partial class App : Application
{
...
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
User currentUser = e.User;
...
}
...
}