获取当前登录的OS用户在Adobe AIR [英] Get the current logged in OS user in Adobe Air
问题描述
我需要在当前登录的用户在我的空气/ Flex应用程序的名称。该应用程序将只被部署在Windows机器上。我想我可以通过regexing用户目录实现这一点,但我开放给其他的方式。
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.
推荐答案
有一对小情侣的清理可以进行。 ..
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;
}
}
}
由于凯文建议,使用文件分割符
使目录拆分跨平台(只是测试在Windows和Mac OS X)。
As Kevin suggested, use File.separator
to make the directory splitting cross-platform (just tested on Windows and Mac OS X).
您不需要使用与resolvePath()
,除非你正在寻找一个孩子。
You don't need to use resolvePath("")
unless you're looking for a child.
此外,使得功能的适当的getter时不需要任何进一步的工作结合。
Also, making the function a proper getter allows binding without any further work.
在上面的例子中,我把它变成一个 UserUtil
类,现在我可以绑定到 UserUtil.currentOSUser
,例如:
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>
这篇关于获取当前登录的OS用户在Adobe AIR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!