How do I get the current username in .NET using C#?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Generic Generics in Managed C++
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
The documentation for Environment.UserName seems to be a bit conflicting:
Environment.UserName Property
On the same page it says:
AND
If you test Environment.UserName using RunAs, it will give you the RunAs user account name, not the user originally logged on to Windows.
You may also want to try using:
Like this...:
Hope this has been helpful.
I've tried all the previous answers and found the answer on MSDN after none of these worked for me. See 'UserName4' for the correct one for me.
I'm after the Logged in User, as displayed by:
Here's a little function I wrote to try them all. My result is in the comments after each row.
Calling this function returns the logged in username by return.
Update: I would like to point out that running this code on my Local server instance shows me that Username4 returns "" (an empty string), but UserName3 and UserName5 return the logged in User. Just something to beware of.
Get the current Windows username:
This will give you output - your_user_name
For a Windows Forms app that was to be distributed to several users, many of which log in over vpn, I had tried several ways which all worked for my local machine testing but not for others. I came across a Microsoft article that I adapted and works.