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

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

问题描述

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

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

例如。在PHP中,我们有 __ DIR __ 常量,这是reffer到脚本目录。如果我们有两个脚本: ./ 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 )。

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

我如何做到这一点?

推荐答案

test.dart ,当前目录为 / path / to / lib / tests 。所以你应该能够使用相对路径打开 rules.json 。路径是 ../ rules / rules.json (或者当然, .. \rules\rules.json 在Windows上,您可以从获取分隔符 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, ..\rules\rules.json on Windows, you can get the separator from Platform.pathSeparator).

或者,您可以获得 Uri 当前脚本(如果平台支持) 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天全站免登陆