2013-04-29 23 views
24

我想在我的WPF應用程序中顯示用戶名,它應該從當前用戶登錄時獲取用戶名?如何獲取WPF中的Windows用戶名

所以我的問題我們如何獲得從WPF應用程序登錄用戶的Windows用戶名?

回答

40
System.Security.Principal.WindowsIdentity.GetCurrent().Name 

我還發現:

Environment.UserName 

System.Windows.Forms.SystemInformation.UserName 

我不能嘗試它,以便檢查自己的結果。

補充: 完整的用戶名:

Imports System.DirectoryServices.AccountManagement 

Dim userFullName As String = UserPrincipal.Current.DisplayName 
+2

System.Security.Principal.WindowsIdentity.GetCurrent()。Name,This one is working謝謝 – Ujjwal27 2013-04-29 08:18:24

+1

如何獲得完整的用戶名而不是用戶ID示例:而不是顯示Ujjwal27顯示「Ujjwal Vaddepati」 – Ujjwal27 2013-04-29 09:28:19

+1

System .DirectoryServices.AccountManagement.UserPrincipal.Current.UserPrincipalName對於在ADAL中提供有意義的提示也很有用。 – 2014-01-10 20:53:15

7

呼叫WindowsIdentity.GetCurrent()獲得Windows用戶身份。

您可以從中獲得該名稱。

相關問題