- Home /
Get current username
Hi, i'm trying to get the current username on Windows and Mac.
For Mac i found Environment.UserName but it returned NULL
Can anyone help me to do this on windows and Mac using Javascript code?
Thanks.
Answer by dannyskim · Jan 02, 2012 at 09:23 PM
Environment.Username is the appropriate call, it works perfectly fine for me inside the editor.
I'm making an assumption here, but have you made sure you included the import System; at the top of your javascript file?
Sorry i answered you in another in the next posted answer =P Thanks.
Answer by cbordeman_unity · May 18, 2019 at 12:42 PM
The current user can mean many things. If you just want the profile logged into the device,
string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
This gets the user the game is running as, which is safer than Environment.Username.
If your game is using OAuth or its own internal login system, then that won't work.
It should be fairly obvious by the name, but that API isn't cross-platform. Use it with care if you want your game to compile on non-windows machines.
Answer by jaramillo · Jan 02, 2012 at 09:33 PM
Yes, i did. i don't know why it throws NULL but i already solved it. Thanks ;)
If anyone has this problem here's the solution.
System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) returns the current username on Windows and Mac
@cbordeman_unity , This post from more than 7 years ago that you reacted to is not something to LOL about.
This could potentially return the folder c:\users\\Documents. With some fiddling you can extract the username. Not very elegant, but it might be a solution if all else fails.
Your answer
Follow this Question
Related Questions
Sharing a project folder between Mac and Windows ? 0 Answers
Mac and Windows Dropbox problem 1 Answer
Can keyboard multimedia key events be captured? 0 Answers
multitouch on desktop possible 1 Answer