CurrentUser

  • Thread starter Thread starter reidarT
  • Start date Start date
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, ByRef nSize As Integer) As Integer

Public Function GetUserName() As String
Dim iReturn As Integer
Dim userName As String
userName = New String(CChar(" "), 50)
iReturn = GetUserName(userName, 50)
GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))
End Function

reidarT
 
Or: My.User.Name
Environment.UserName

regards

Michel Posseth [MCP]


reidarT said:
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, ByRef nSize As Integer) As Integer

Public Function GetUserName() As String
Dim iReturn As Integer
Dim userName As String
userName = New String(CChar(" "), 50)
iReturn = GetUserName(userName, 50)
GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))
End Function

reidarT
 
I use the following code.

Public Shared Function CurrentUser() As String
Static user As String = ""
If user = "" Then
Dim WindowsUser As System.Security.Principal.WindowsIdentity = _
System.Security.Principal.WindowsIdentity.GetCurrent
user = WindowsUser.Name.ToUpper
Dim i As Integer = InStrRev(user, "\")
If i > 0 Then user = Mid$(user, i + 1)
End If
Return user
End Function

Mike Ober.
 
no they are not the same :-)

on a computer in a domain

Environment.UserName returns the username ( "Michel")

while My.User.Name returns ("domainName\Michel"

ofcourse you could split this but why would you ? if there is an alternative
:-)

regards

Michel Posseth



Theo Verweij said:
Or: My.User.Name
Environment.UserName

regards

Michel Posseth [MCP]


reidarT said:
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, ByRef nSize As Integer) As Integer

Public Function GetUserName() As String
Dim iReturn As Integer
Dim userName As String
userName = New String(CChar(" "), 50)
iReturn = GetUserName(userName, 50)
GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))
End Function

reidarT

"reidarT" <[email protected]> skrev i melding
How do I get current user logged in with vb.net code?
reidarT
 
Your are right.

My.User.Name = Environment.UserDomain & "\"& Environment.UserName

But both are much simpler than using an API call :-)

regards,
Theo Verweij
no they are not the same :-)

on a computer in a domain

Environment.UserName returns the username ( "Michel")

while My.User.Name returns ("domainName\Michel"

ofcourse you could split this but why would you ? if there is an alternative
:-)

regards

Michel Posseth



Theo Verweij said:
Or: My.User.Name
Environment.UserName

regards

Michel Posseth [MCP]


:

Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA"
(ByVal lpBuffer As String, ByRef nSize As Integer) As Integer

Public Function GetUserName() As String
Dim iReturn As Integer
Dim userName As String
userName = New String(CChar(" "), 50)
iReturn = GetUserName(userName, 50)
GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))
End Function

reidarT

"reidarT" <[email protected]> skrev i melding
How do I get current user logged in with vb.net code?
reidarT
 
Back
Top