I need the name of the current logged in user in my Air/Flex application. The application will only be deployed on Windows machines. I think I could attain this by regexing the User directory, but am open to other ways.
5 Answers
There's a couple of small cleanups you can make...
package
{
import flash.filesystem.File;
public class UserUtil
{
public static function get currentOSUser():String
{
var userDir:String = File.userDirectory.nativePath;
var userName:String = userDir.substr(userDir.lastIndexOf(File.separator) + 1);
return userName;
}
}
}
As Kevin suggested, use File.separator
to make the directory splitting cross-platform (just tested on Windows and Mac OS X).
You don't need to use resolvePath("")
unless you're looking for a child.
Also, making the function a proper getter allows binding without any further work.
In the above example I put it into a UserUtil
class, now I can bind to UserUtil.currentOSUser
, e.g:
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Label text="{UserUtil.currentOSUser}"/>
</mx:WindowedApplication>

- 6,324
- 5
- 28
- 44

- 3,395
- 1
- 25
- 26
-
2This solution doesnť work when the user has different login name and home directory name, which is common when OS is reinstalled or migrated. Does anyone know another solution. Please help. – Feb 09 '11 at 23:46
Also I would try:
File.userDirectory.name
But I don't have Air installed so I can't really test this...

- 13,044
- 11
- 55
- 76
-
Works perfectly fine on Windows XP. Will it provide the same for Vista, Windows 7 and iMac? – midhunhk Sep 14 '11 at 08:15
-
This isn't the prettiest approach, but if you know your AIR app will only be run in a Windows environment it works well enough:
public var username:String;
public function getCurrentOSUser():void
{
var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();
var file:File = new File("C:/WINDOWS/system32/whoami.exe");
nativeProcessStartupInfo.executable = file;
process = new NativeProcess();
process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);
process.start(nativeProcessStartupInfo);
}
public function onOutputData(event:ProgressEvent):void
{
var output:String = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable);
this.username = output.split('\\')[1];
trace("Got username: ", this.username);
}

- 166
- 2
- 7
Here is a solution that works in XP / Vista, but is definitely expandable to OSX, linux, I'd still be interested in another way.
public static function GetCurrentOSUser():String{
// XP & Vista only.
var userDirectory:String = File.userDirectory.resolvePath("").nativePath;
var startIndex:Number = userDirectory.lastIndexOf("\\") + 1
var stopIndex:Number = userDirectory.length;
var user = userDirectory.substring(startIndex, stopIndex);
return user;
}

- 6,324
- 5
- 28
- 44

- 19,465
- 20
- 98
- 152
-
1May want to replace `"\\"` with `File.separator` to make it work on linux. – Kevin Feb 09 '11 at 23:44
Update way later: there's actually a built in function to get the current user. I think it's in nativeApplication.

- 19,465
- 20
- 98
- 152
-
I need to get the current user too, but can't find that function... can you share the solution? – Cosma Colanicchia Jul 31 '09 at 13:04
-
There is nothing mentioned in http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/desktop/NativeApplication.html – merlinc Mar 30 '11 at 16:00