获取Windows用户显示名称

时间:2014-03-05 03:13:01

标签: c# .net vb.net winforms

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

enter image description here

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

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

3 个答案:

答案 0 :(得分:57)

您应该使用UserPrincipal.DisplayName

System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName

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

答案 1 :(得分:-2)

试试这个

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);
    }
}

答案 2 :(得分:-3)

这包含在System.DirectoryServices命名空间中,因此您需要在using部分添加此内容。

然后您可以使用System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName返回Display Name。这通常显示在“开始”菜单中。

相关问题