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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

sms 1
combobox dropdown 2
Compressing (zipping) files 6
left, mid and right in vb.net 7
Adding images to image folder in vb.net 1
Open a form 6
Sending email 2
Time (hour) question 1

Back
Top