Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HOW TO REQUEST FROM VB WINDOWS USER NAME? 1

Status
Not open for further replies.

IrinaK

Programmer
Joined
May 21, 2001
Messages
33
Location
US
Is it possible to find out from VB application the user name, who was logged in into the Windows NT system?

Thank you very much.
 
Actually, SiriusBlackOp, I would disagree with you.

The User's Name, is available from:

[tt]Environment.UserName[/tt] (both 2003 and 2005)

My.User.Name (2005 only) returns the same as

[tt]Environment.UserDomainName + "\" + Environment.UserName[/tt] (2003 and 2005).

My.??? doesn't appear to provide access to just the User's Name, only providing DomainName\UserName


Hope this helps.




[vampire][bat]
 
In fact I was wrong, My.??? does provide this information (sort of ...)

The following uses the computer's environment variables (2005 only):

[tt]
MessageBox.Show(My.Application.GetEnvironmentVariable("USERNAME"))
MessageBox.Show(My.Application.GetEnvironmentVariable("USERDOMAIN"))
MessageBox.Show(My.Application.GetEnvironmentVariable("COMPUTERNAME"))
MessageBox.Show(My.Application.GetEnvironmentVariable("LOGONSERVER"))
[/tt]

for 2003, replace [tt]My.Application[/tt] with [tt]Environment[/tt], for example [tt]Environment.GetEnvironmentVariable("USERNAME")[/tt]


Hope this helps.

[vampire][bat]
 
As an aside, if you need to know what environment variables are available and what information they contain, open a CommandPrompt window and enter SET.


Hope this helps.

[vampire][bat]
 
Thank you for your help.
But unfortunately it doesn't work for me.
When I am trying this:
My.Application.GetEnvironmentVariable("USERNAME") - I get errorr message: that I need and object.

When I tried this:
GetEnvironmentVariable("USERNAME") - I get errorr message: sub or function not defined.
 
What version of VB.NET are you using?

Version 2003:

[tt]Environment.GetEnvironmentVariable("USERNAME")[/tt]


=======================================================

Version 2005:

[tt]My.Application.GetEnvironmentVariable("USERNAME")[/tt]


Hope this helps.





[vampire][bat]
 
Sorry, I clicked Submit too soon - I meant to add that the 2003 version that I posted above works in both 2003 and 2005.


Hope this helps.

[vampire][bat]
 
Thank you very much earthandfire.
It didn't work for me, but thanks to you I found in VB function "Environ", which give me user name, user domain and computer name.

Thanks again.
 
Interesting. I didn't know that the Environ function had been retained in VB.NET, I thought it was just in VB5/6 etc and VBA.

I'm not sure why you couldn't get either:

Environment.GetEnvironmentVariable("USERNAME")

or:

Environment.UserName

to work, as they should work with VB.NET versions 2003 and 2005 - but at least you've got a working solution.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top