Thursday 28 April 2016

c# - How to get Windows start username?





Well, I posted this question earlier but I haven't got the correct answer before it was marked as a duplicate. I think this is a different question, all the methods that I used returns a wrong output.



I wanted to know how to solve this. A sample output is here:




enter image description here



the code:



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)


' System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName()
End Sub

End Class


I don't even know the correct term / name for this Windows username.


Answer



Use System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName




As noted by the replies on this thread, you can use this by adding a reference to System.DirectoryServices.AccountManagement.dll in your project.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...