<p>I need the name of the current logged in user in my <strong>Air/Flex</strong> 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.</p>

[{'answer_id': 1380, 'author': 'Shawn', 'author_id': 26, 'author_profile': 'https://Stackoverflow.com/users/26', 'pm_score': 1, 'selected': False, 'text': '<p>Here is a solution that works in XP / Vista, but is definitely expandable to OSX, linux, I\'d still be interested in another way.</p>\n\n<pre><code>public static function GetCurrentOSUser():String{\n    // XP &amp; Vista only.\n    var userDirectory:String = File.userDirectory.resolvePath("").nativePath;\n    var startIndex:Number = userDirectory.lastIndexOf("\\\\") + 1\n    var stopIndex:Number = userDirectory.length;\n    var user = userDirectory.substring(startIndex, stopIndex);\n\n    return user;\n}\n</code></pre>\n'}, {'answer_id': 1397, 'author': 'Kevin', 'author_id': 40, 'author_profile': 'https://Stackoverflow.com/users/40', 'pm_score': 3, 'selected': False, 'text': "<p>Also I would try:</p>\n\n<pre><code>File.userDirectory.name\n</code></pre>\n\n<p>But I don't have Air installed so I can't really test this...</p>\n"}, {'answer_id': 28034, 'author': 'ianmjones', 'author_id': 3023, 'author_profile': 'https://Stackoverflow.com/users/3023', 'pm_score': 3, 'selected': False, 'text': '<p>There\'s a couple of small cleanups you can make...</p>\n\n<pre><code>package\n{\n    import flash.filesystem.File;\n\n    public class UserUtil\n    {\n        public static function get currentOSUser():String\n        {\n            var userDir:String = File.userDirectory.nativePath;\n            var userName:String = userDir.substr(userDir.lastIndexOf(File.separator) + 1);\n            return userName;\n        }\n    }\n}\n</code></pre>\n\n<p>As Kevin suggested, use <code>File.separator</code> to make the directory splitting cross-platform (just tested on Windows and Mac OS X).</p>\n\n<p>You don\'t need to use <code>resolvePath("")</code> unless you\'re looking for a child.</p>\n\n<p>Also, making the function a proper getter allows binding without any further work.</p>\n\n<p>In the above example I put it into a <code>UserUtil</code> class, now I can bind to <code>UserUtil.currentOSUser</code>, e.g:</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;\n&lt;mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"&gt;\n    &lt;mx:Label text="{UserUtil.currentOSUser}"/&gt; \n&lt;/mx:WindowedApplication&gt;\n</code></pre>\n'}, {'answer_id': 417427, 'author': 'Shawn', 'author_id': 26, 'author_profile': 'https://Stackoverflow.com/users/26', 'pm_score': -1, 'selected': False, 'text': "<p>Update way later: there's actually a built in function to get the current user. I think it's in nativeApplication.</p>\n"}, {'answer_id': 5024862, 'author': 'Not Mandatory', 'author_id': 234764, 'author_profile': 'https://Stackoverflow.com/users/234764', 'pm_score': 3, 'selected': False, 'text': '<p>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:</p>\n\n<pre><code>public var username:String;\n\npublic function getCurrentOSUser():void\n{       \n   var nativeProcessStartupInfo:NativeProcessStartupInfo = new NativeProcessStartupInfo();  \n   var file:File = new File("C:/WINDOWS/system32/whoami.exe");\n   nativeProcessStartupInfo.executable = file;\n\n   process = new NativeProcess();       \n   process.addEventListener(ProgressEvent.STANDARD_OUTPUT_DATA, onOutputData);\n   process.start(nativeProcessStartupInfo);\n}\n\npublic function onOutputData(event:ProgressEvent):void\n{           \n   var output:String = process.standardOutput.readUTFBytes(process.standardOutput.bytesAvailable);\n   this.username = output.split(\'\\\\\')[1];\n   trace("Got username: ", this.username);\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/1376', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/26/']