在 Haxe 中检索标准用户信息

Retrieve Standard User Information in Haxe

我终于有时间开始玩 Haxe,我觉得这是一个很棒的概念,但我在尝试推断如何获取当前系统用户名或 "special folder" 位置时遇到了麻烦。

在我的例子中,用户的主文件夹、文档文件夹或更新文件夹。我的目标语言是 Java,后来可能是 C#...但是我没有找到直接查询系统以获取此信息的真正方法。到目前为止,我所发现的只是如何查询环境变量......具有大量可变性。

Haxe std lib 不提供此类特定功能。但是,这并不妨碍我们使用特定于目标的 API。

#if java
import java.lang.System;
#elseif cs
import cs.system.Environment;
import cs.system.Environment.Environment_SpecialFolder;
#end
class Test {
    static function main() {
        #if java
            //http://docs.oracle.com/javase/6/docs/api/java/lang/System.html#getProperties%28%29
            trace(System.getProperty("user.home")); //Test.hx:11: C:\Users\Andy
        #elseif cs
            //https://msdn.microsoft.com/en-us/library/system.environment.specialfolder
            trace(Environment.GetFolderPath(Environment_SpecialFolder.UserProfile)); //Test.hx:14: C:\Users\Andy
        #end
    }
}

build.hxml:

-main Test
-java bin
-cmd java -jar bin/Test.jar

--next

-main Test
-cs bin
# Environment_SpecialFolder.UserProfile is available since .net 4.0
-D net-ver=40
-cmd bin\bin\Test.exe