【问题标题】:How can i get the Complete user name in Windows 7 using WPF?如何使用 WPF 在 Windows 7 中获取完整的用户名?
【发布时间】:2013-04-29 13:58:14
【问题描述】:

我已经尝试过这个链接,但我只得到用户 ID 我想要用户的完整名称

Username in WPF

【问题讨论】:

  • 您希望用户名来自哪里?是否在数据库中,是否在用户登录时检索它?
  • 我正在尝试在我的 WPF 应用程序中获取它,当前从 windows 机器登录的用户名(完整的用户名)

标签: c# wpf


【解决方案1】:

这将为您提供在计算机上登录的用户的全名

Thread.GetDomain().SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
// or, if you're in Asp.Net with windows authentication you can use:
// WindowsPrincipal principal = (WindowsPrincipal)User;
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain))
{
    UserPrincipal up = UserPrincipal.FindByIdentity(pc, principal.Identity.Name);
    thisUsername = up.DisplayName;
    // or return up.GivenName + " " + up.Surname;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多