在 Adob​​e Air 中获取当前登录的操作系统用户 [英] Get the current logged in OS user in Adobe Air

查看:26
本文介绍了在 Adob​​e Air 中获取当前登录的操作系统用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要 Air/Flex 应用程序中当前登录用户的名称.该应用程序将仅部署在 Windows 机器上.我想我可以通过对用户目录进行正则表达式来实现这一点,但我对其他方式持开放态度.

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

正如 Kevin 建议的,使用 File.separator 使目录拆分跨平台(刚刚在 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>

这篇关于在 Adob​​e Air 中获取当前登录的操作系统用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆