[英]Get Windows User Display Name
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.不是用户名,而是显示名称,如下面的屏幕截图所示 - 在任何 Windows Vista/7 计算机的开始菜单上都可以看到。
I tried a bunch of different suggestions from other questions, but they all show the username , not the display name .我尝试了来自其他问题的一系列不同建议,但它们都显示username ,而不是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
You should use UserPrincipal.DisplayName
:您应该使用UserPrincipal.DisplayName
:
System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName
To do so, you'll need to and add a reference to System.DirectoryServices.AccountManagement.dll
from your project.为此,您需要从项目中添加对System.DirectoryServices.AccountManagement.dll
的引用。
Try this尝试这个
http://msdn.microsoft.com/en-us/library/sfs49sw0(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/sfs49sw0(v=vs.110).aspx
using System.IO;
using System;
using System.Security.Principal;
class Program
{
static void Main()
{
Console.WriteLine(WindowsIdentity.GetCurrent().Name);
}
}
This is contained within the System.DirectoryServices
namespace so you need to add this in the using
section.它包含在System.DirectoryServices
命名空间中,因此您需要在using
部分中添加它。
Then you can use System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName
which returns the Display Name
.然后您可以使用System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName
返回Display Name
。 This is typically shown in the Start Menu.这通常显示在“开始”菜单中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.