Home » Questions » Computers [ Ask a new question ]

Get the current logged in OS user in Adobe Air

Get the current logged in OS user in Adobe Air

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.

Asked by: Guest | Views: 137
Total answers/comments: 3
Guest [Entry]

"Also I would try:


But I don't have Air installed so I can't really test this..."
Guest [Entry]

"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);

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);
Guest [Entry]

"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;