tags:

views:

737

answers:

4

how to get current username in .net using c#?

+6  A: 
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
Juan Manuel
Hi Juan, I got this error: Error 2 Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Kombucha
How is this different than `Environment.UserName`?
280Z28
@Yonita... `string username =.....`??
280Z28
Well, you have to assign that value to something. Edited.
Juan Manuel
@Yonita did you assign the value to something?
ghills
You are ALL correct, I didn't - It's working now :) thank you sO much smart people in stackoverflow. The Best +1
Kombucha
+12  A: 

Try Environment.UserName

JaredPar
Or, `string userName = Environment.UserName;`
Donut
+1  A: 

System.Security.Principal.WindowsIdentity.GetCurrent().Name

that be the logon name

John Cuckaroo
+1  A: 

You may also want to try using:

Environment.UserName;

Like this...:

string j = "Your WindowsXP Account Name is: " + Environment.UserName;

Hope this has been helpful.

baeltazor