Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the current logged in OS user in Adobe Air

Tags:

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.

like image 480
Shawn Avatar asked Aug 04 '08 16:08

Shawn


2 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>
like image 74
ianmjones Avatar answered Oct 17 '22 06:10

ianmjones


Also I would try:

File.userDirectory.name

But I don't have Air installed so I can't really test this...

like image 28
Kevin Avatar answered Oct 17 '22 08:10

Kevin