更新时间:2023-12-03 19:08:22
有一对小情侣的清理可以进行。 ..
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>