Home All Groups Group Topic Archive Search About
Author
15 Aug 2006 11:51 AM
reidarT
How do I get current user logged in with vb.net code?
reidarT

Author
15 Aug 2006 12:03 PM
reidarT
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

Show quoteHide quote
"reidarT" <rei***@eivon.no> skrev i melding
news:etAJeEGwGHA.4460@TK2MSFTNGP04.phx.gbl...
> How do I get current user logged in with vb.net code?
> reidarT
>
Author
15 Aug 2006 1:04 PM
Michel Posseth [MCP]
Environment.UserName

regards

Michel Posseth [MCP]


Show quoteHide quote
"reidarT" wrote:

>
>     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" <rei***@eivon.no> skrev i melding
> news:etAJeEGwGHA.4460@TK2MSFTNGP04.phx.gbl...
> > How do I get current user logged in with vb.net code?
> > reidarT
> >
>
>
>
Author
15 Aug 2006 5:54 PM
Theo Verweij
Or: My.User.Name

Michel Posseth [MCP] wrote:
Show quoteHide quote
>
> Environment.UserName
>
> regards
>
> Michel Posseth [MCP]
>
>
> "reidarT" wrote:
>
>>     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" <rei***@eivon.no> skrev i melding
>> news:etAJeEGwGHA.4460@TK2MSFTNGP04.phx.gbl...
>>> How do I get current user logged in with vb.net code?
>>> reidarT
>>>
>>
>>
Author
15 Aug 2006 8:02 PM
Michael D. Ober
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.

Show quoteHide quote
"Theo Verweij" <tverw***@xs4all.nl> wrote in message
news:e4svNPJwGHA.724@TK2MSFTNGP05.phx.gbl...
> Or: My.User.Name
>
> Michel Posseth [MCP] wrote:
>>
>> Environment.UserName
>>
>> regards
>>
>> Michel Posseth [MCP] "reidarT" wrote:
>>
>>>     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" <rei***@eivon.no> skrev i melding
>>> news:etAJeEGwGHA.4460@TK2MSFTNGP04.phx.gbl...
>>>> How do I get current user logged in with vb.net code?
>>>> reidarT
>>>>
>>>
>>>
Author
16 Aug 2006 6:32 AM
Michel Posseth [MCP]
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  



Show quoteHide quote
"Theo Verweij" wrote:

> Or: My.User.Name
>
> Michel Posseth [MCP] wrote:
> >
> > Environment.UserName
> >
> > regards
> >
> > Michel Posseth [MCP]
> >
> >
> > "reidarT" wrote:
> >
> >>     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" <rei***@eivon.no> skrev i melding
> >> news:etAJeEGwGHA.4460@TK2MSFTNGP04.phx.gbl...
> >>> How do I get current user logged in with vb.net code?
> >>> reidarT
> >>>
> >>
> >>
>
Author
16 Aug 2006 8:07 AM
Theo Verweij
Your are right.

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

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

regards,
Theo Verweij

Michel Posseth [MCP] wrote:
Show quoteHide quote
> 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" wrote:
>
>> Or: My.User.Name
>>
>> Michel Posseth [MCP] wrote:
>>> Environment.UserName
>>>
>>> regards
>>>
>>> Michel Posseth [MCP]
>>>
>>>
>>> "reidarT" wrote:
>>>
>>>>     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" <rei***@eivon.no> skrev i melding
>>>> news:etAJeEGwGHA.4460@TK2MSFTNGP04.phx.gbl...
>>>>> How do I get current user logged in with vb.net code?
>>>>> reidarT
>>>>>
>>>>