Search code examples
haxehxcpp

Haxe (hxcpp) – Path relative to executable path


Say I have following file structure of my application:

Data/prefs.ini
executable.exe

How can I open prefs.ini providing relative path to it from executable.exe is always the same (known at compile time)? Or how can I get absolute path of executable.exe? I need it to work on Linux, Mac and Windows.


Solution

  • There is an exact haxe API for that: Sys.executablePath() (doc)

    To get a path relative to it:

    import haxe.io.Path;
    class Test {
        static public function relToExe(path:String):String {
            return Path.join([Path.directory(Sys.executablePath()), path]);
        }
        static function main() {
            trace(relToExe("something"));
        }
    }