获取 Windows 用户显示名称 [英] Get Windows User Display Name

查看:22
本文介绍了获取 Windows 用户显示名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取登录用户的显示名称?不是用户名,而是显示名称,如下面的屏幕截图所示 - 以及任何 Windows Vista/7 计算机的开始菜单中所示.

How do I get the display name of the user that is logged in? Not the username, but the display name, such as is shown in the screenshot below - and as seen on the start menu in any Windows Vista/7 computer.

我尝试了来自其他问题的一系列不同建议,但它们都显示用户名,而不是显示名称.您可以在上面的屏幕截图中看到这些尝试的结果.

I tried a bunch of different suggestions from other questions, but they all show the username, not the display name. You can see the results of these attempts in the above screenshot.

Imports System.Security.Principal
Imports System.Threading
Imports System.IO
Imports System

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MsgBox("1: " & System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString & vbCrLf & _
               "2: " & Environment.UserDomainName & vbCrLf & _
               "3: " & WindowsIdentity.GetCurrent().Name & vbCrLf & _
                "4: " & Thread.CurrentPrincipal.Identity.Name & vbCrLf & _
               "5: " & Environment.UserName & vbCrLf & _
               "6: " & My.User.Name & vbCrLf &
                "7: " & My.Computer.Name)

    End Sub

End Class

推荐答案

您应该使用 UserPrincipal.DisplayName:

You should use UserPrincipal.DisplayName:

System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName

为此,您需要从项目中添加对 System.DirectoryServices.AccountManagement.dll 的引用.

To do so, you'll need to and add a reference to System.DirectoryServices.AccountManagement.dll from your project.

这篇关于获取 Windows 用户显示名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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