By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,558 Members | 1,673 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,558 IT Pros & Developers. It's quick & easy.

Getting current user name

P: n/a
How can I get the login name of the user currently logged into Windows as a
string?

Thanks,
Steve Enzer
Feb 16 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Hi Steve,

System.Environment.UserName

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Steve Enzer" <no****@nospam.com> escribió en el mensaje
news:e1**************@TK2MSFTNGP10.phx.gbl...
How can I get the login name of the user currently logged into Windows as
a string?

Thanks,
Steve Enzer

Feb 16 '06 #2

P: n/a
"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in message news:Op**************@TK2MSFTNGP10.phx.gbl...
Hi Steve,

System.Environment.UserName
How simple. Ive been using
System.Security.Principal.WindowsIdentity.GetCurre nt().Name

--

Al Reid


--

Best regards,

Carlos J. Quintero

Feb 16 '06 #3

P: n/a
Hi,

Imports System.Security
Imports System.Security.Principal

AppDomain.CurrentDomain.SetPrincipalPolicy(Princip alPolicy.WindowsPrincipal)
Dim myID As WindowsIdentity = WindowsIdentity.GetCurrent()
Msgbox("The Current User is : " & myID.Name)

Regards,
Cerebrus.

Feb 16 '06 #4

P: n/a
Well, in .NET 2.0 is simpler: My.User.Name, although it returns the domain
too.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com

"Al Reid" <ar*****@reidDASHhome.com> escribió en el mensaje
news:Ov*************@TK2MSFTNGP12.phx.gbl...
"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:Op**************@TK2MSFTNGP10.phx.gbl...
Hi Steve,

System.Environment.UserName


How simple. Ive been using
System.Security.Principal.WindowsIdentity.GetCurre nt().Name


Feb 16 '06 #5

P: n/a
I saw your post just now and have a question, Carlos.

The definition of the Environment.Username property suggests that it
would get the name of the User who launched the current thread or
process. Now, in some cases, that username could be SYSTEM or
LOCALSERVICE or NETWORKSERVICE.

But I think that the WindowsIdentity name would always be the name of
the currently logged on user.

Please tell me if I am right (or correct me if I'm wrong ! ), and if
so, then Environment.Username would not be such a good method of
getting this information, after all.

Thanks,

Cerebrus.

Feb 16 '06 #6

P: n/a
FYI, the My.User.Name functionality does not work for a console app.

"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Well, in .NET 2.0 is simpler: My.User.Name, although it returns the domain
too.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com

"Al Reid" <ar*****@reidDASHhome.com> escribió en el mensaje
news:Ov*************@TK2MSFTNGP12.phx.gbl...
"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:Op**************@TK2MSFTNGP10.phx.gbl...
Hi Steve,

System.Environment.UserName


How simple. Ive been using
System.Security.Principal.WindowsIdentity.GetCurre nt().Name

Feb 16 '06 #7

P: n/a
Imports System.Security.Principal

'Get the current identity and put it into an identity object.
Dim MyIdentity As WindowsIdentity =
WindowsIdentity.GetCurrent()

'Put the previous identity into a principal object.
Dim MyPrincipal As New WindowsPrincipal(MyIdentity)

'Principal values.
Dim PrincipalName As String = MyPrincipal.Identity.Name
Dim PrincipalType As String =
MyPrincipal.Identity.AuthenticationType
Dim PrincipalAuth As String =
MyPrincipal.Identity.IsAuthenticated.ToString()

'Identity values.
Dim IdentName As String = MyIdentity.Name
Dim IdentType As String = MyIdentity.AuthenticationType
Dim IdentIsAuth As String =
MyIdentity.IsAuthenticated.ToString()
Dim ISAnon As String = MyIdentity.IsAnonymous.ToString()
Dim IsG As String = MyIdentity.IsGuest.ToString()
Dim IsSys As String = MyIdentity.IsSystem.ToString()
Dim Token As String = MyIdentity.Token.ToString()
Regards

Ernesto

Feb 16 '06 #8

P: n/a
Thanks for all the suggestions!

Steve
Feb 16 '06 #9

P: n/a
The My namespace doesn't exist for console applications.

"Joe Sutphin" <jo********@earthlink.net> wrote in message
news:uZ*************@tk2msftngp13.phx.gbl...
FYI, the My.User.Name functionality does not work for a console app.

"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Well, in .NET 2.0 is simpler: My.User.Name, although it returns the
domain too.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com

"Al Reid" <ar*****@reidDASHhome.com> escribió en el mensaje
news:Ov*************@TK2MSFTNGP12.phx.gbl...
"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:Op**************@TK2MSFTNGP10.phx.gbl...
Hi Steve,

System.Environment.UserName

How simple. Ive been using
System.Security.Principal.WindowsIdentity.GetCurre nt().Name


Feb 16 '06 #10

P: n/a
"AMDRIT" <am****@hotmail.com> schrieb:
The My namespace doesn't exist for console applications.


Well, with conditional compilation constants it's maybe possible to add it
to console applications too, but I fear that this would be justifiable only
for using a simple method which is mainly a wrapper around .NET Framework
methods.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Feb 16 '06 #11

P: n/a
Hi,

Yes, Environment.Username makes internally a call to the Win32 API function
GetUserName and according to the docs
(http://msdn.microsoft.com/library/de...etusername.asp)
it retrieves the user associated with the current thread. So, in most cases
it will do the trick but if you are calling it from a Windows service it
won´t do the trick, although it doesn´t make much sense to do it from a
Windows service (by definition)...

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Cerebrus99" <zo*****@sify.com> escribió en el mensaje
news:11*********************@g43g2000cwa.googlegro ups.com...
I saw your post just now and have a question, Carlos.

The definition of the Environment.Username property suggests that it
would get the name of the User who launched the current thread or
process. Now, in some cases, that username could be SYSTEM or
LOCALSERVICE or NETWORKSERVICE.

But I think that the WindowsIdentity name would always be the name of
the currently logged on user.

Please tell me if I am right (or correct me if I'm wrong ! ), and if
so, then Environment.Username would not be such a good method of
getting this information, after all.

Thanks,

Cerebrus.

Feb 17 '06 #12

P: n/a
Is there also a (simple) way to have Windows authenticate a Username /
Password combination?
I need that for an application security lock (after xxx minutes of
inactivity)

"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
Well, in .NET 2.0 is simpler: My.User.Name, although it returns the domain
too.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com

"Al Reid" <ar*****@reidDASHhome.com> escribió en el mensaje
news:Ov*************@TK2MSFTNGP12.phx.gbl...
"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:Op**************@TK2MSFTNGP10.phx.gbl...
Hi Steve,

System.Environment.UserName


How simple. Ive been using
System.Security.Principal.WindowsIdentity.GetCurre nt().Name

Feb 17 '06 #13

P: n/a
No, duh !

"AMDRIT" <am****@hotmail.com> wrote in message
news:eP**************@TK2MSFTNGP12.phx.gbl...
The My namespace doesn't exist for console applications.

"Joe Sutphin" <jo********@earthlink.net> wrote in message
news:uZ*************@tk2msftngp13.phx.gbl...
FYI, the My.User.Name functionality does not work for a console app.

"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:%2****************@TK2MSFTNGP14.phx.gbl...
Well, in .NET 2.0 is simpler: My.User.Name, although it returns the
domain too.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com

"Al Reid" <ar*****@reidDASHhome.com> escribió en el mensaje
news:Ov*************@TK2MSFTNGP12.phx.gbl...
"Carlos J. Quintero [VB MVP]" <ca*****@NOSPAMsogecable.com> wrote in
message news:Op**************@TK2MSFTNGP10.phx.gbl...
> Hi Steve,
>
> System.Environment.UserName

How simple. Ive been using
System.Security.Principal.WindowsIdentity.GetCurre nt().Name



Feb 20 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.