在 Dart 中获取脚本路径(PHP 中的模拟 __DIR__ 常量) [英] Get script path in Dart (analog __DIR__ constant in PHP)

查看:31
本文介绍了在 Dart 中获取脚本路径(PHP 中的模拟 __DIR__ 常量)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Dart 中获取当前脚本(不可执行)的路径?

How I can get path of the current script (not executable) in Dart?

例如.在 PHP 中,我们有 __DIR__ 常量,它指的是脚本目录.如果我们有两个脚本:./run.php./lib/test.php;在 ./run.php 中,我们只包含 ./lib/test.php../lib/test.php 输出 __DIR__ 中的代码.如果我们运行 ./run.php 然后我们得到输出 /path/to/lib/test.php.

For example. In PHP we have __DIR__ constant, that reffer to script directory. If we have two scripts: ./run.php and ./lib/test.php; and in ./run.php we just include ./lib/test.php. Code in ./lib/test.php output __DIR__. if we run ./run.php then we get in output /path/to/lib/test.php.

在 Dart 中我们可以获取当前运行脚本的目录,但是如何获取当前脚本的目录?

In Dart we can get directory of the current run script, but how I can get directory of the current script?

我需要这个,因为我试图从库中获取 json-file(放在库中的).

I need in this, because I'm try to get json-file (that placed in library) from the library.

我该怎么做?

推荐答案

你说过当你运行 test.dart 时,当前目录是 /path/to/lib/测试.所以你应该能够使用相对路径来打开 rules.json.路径是 ../rules/rules.json(当然,在 Windows 上,.. ules ules.json,你可以从 Platform.pathSeparator).

You've said that when you run test.dart, the current directory is /path/to/lib/tests. So you should be able to use a relative path to open rules.json. The path is ../rules/rules.json (or of course, .. ules ules.json on Windows, you can get the separator from Platform.pathSeparator).

或者,显然你可以得到一个 来自 Platform.script.

Alternately, apparently you can get a Uri for the current script (if supported by the platform) from Platform.script.

这篇关于在 Dart 中获取脚本路径(PHP 中的模拟 __DIR__ 常量)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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