在Dart SDK中如何获取Windows系统下不包含开头"斜杠"的路径字符串?

3
import 'dart:io';

void main() {
  var path = Platform.script.path;
  print(path);
}

输出

/C:/Users/user/dart/test/bin/test.dart

但是我想要获取

C:/Users/user/dart/test/bin/test.dart

如何获取操作系统特定的路径以便在该操作系统上使用?

P.S.

如果我在不同的平台上运行测试代码,我会得到不同的结果。

因此,需要进行测试。

运行时环境:Dart SDK版本1.1.1(稳定版)

代码:

import 'dart:io';

void main() {
  var path = Platform.script.path;
  print(path);
  // From doc: Creates a new file URI from an absolute or relative file path.
  var uri = new Uri.file(path);
  print(uri.path);
}

Ubuntu 13.10:

/home/andrew/dart/test/bin/test.dart
/home/andrew/dart/test/bin/test.dart

Windows 7:

/C:/Users/user/dart/test/bin/test.dart
Breaking on exception: Illegal argument(s): Illegal character in path}
Unhandled exception:
Illegal argument(s): Illegal character in path}

这种行为阻止我编写跨平台的代码。

Platform.script.path 的值正确为 /C:/Users/user/dart/test/bin/test.dart。URI 是以下文件 URI:file:///C:/Users/user/dart/test/bin/test.dart。有关更多信息,请参见 Windows 中的文件 URI - sgjesse
2个回答

3

这段代码可以在所有平台上运行。

import 'dart:io';

void main() {
  var path = Platform.script.toFilePath();
  print(path);
  var uri = new Uri.file(path);
  print(uri.toFilePath());
}

P.S.

在某些情况下,使用“dart-ext”方案时,在Dart SDK内部也可能会出现类似的异常(“路径中有非法字符”):

Unhandled exception:
Unsupported operation: Illegal character in path}
#0      Uri._checkWindowsPathReservedCharacters.<anonymous closure> (dart:core/uri.dart:395)
#1      ListIterable.forEach (dart:_collection-dev/iterable.dart:39)
#2      Uri._checkWindowsPathReservedCharacters (dart:core/uri.dart:390)
#3      Uri._toWindowsFilePath (dart:core/uri.dart:1018)
#4      Uri.toFilePath (dart:core/uri.dart:992)
#5      _filePathFromUri (dart:builtin:249)
'package:dart_and_cpp_classes/src/cpp_extension.dart': error: line 3 pos 1: library handler failed
import "dart-ext:cpp_extension";
^
'package:dart_and_cpp_classes/cpp_extension.dart': error: line 3 pos 1: library handler failed
import 'package:dart_and_cpp_classes/src/cpp_extension.dart';
^
'file:///C:/Users/user/dart/dart_and_cpp_classes/bin/use_cpp_extension.dart': error: line 1 pos 1: library handler failed
import 'package:dart_and_cpp_classes/cpp_extension.dart';
^

1
请查看路径包导入包:path/path.dart。 我这里没有运行Windows,所以无法验证任何内容。
简短浏览后,我找到了:
/// An enum type describing a "flavor" of path.
abstract class Style {
  /// POSIX-style paths use "/" (forward slash) as separators. Absolute paths
  /// start with "/". Used by UNIX, Linux, Mac OS X, and others.
  static final posix = new PosixStyle();

  /// Windows paths use "\" (backslash) as separators. Absolute paths start with
  /// a drive letter followed by a colon (example, "C:") or two backslashes
  /// ("\\") for UNC paths.
  // TODO(rnystrom): The UNC root prefix should include the drive name too, not
  // just the "\\".
  static final windows = new WindowsStyle();

  /// URLs aren't filesystem paths, but they're supported to make it easier to
  /// manipulate URL paths in the browser.
  ///
  /// URLs use "/" (forward slash) as separators. Absolute paths either start
  /// with a protocol and optional hostname (e.g. `http://dartlang.org`,
  /// `file://`) or with "/".
  static final url = new UrlStyle();

你可以查看我的问题的更新。因此,path 包无法解决这个问题。 - mezoni
我不明白为什么 path 不能在这里帮助。你的 Windows 代码仍然使用 /,而路径文档说明样式 windows 使用 \,绝对路径以驱动器号开头。 - Günter Zöchbauer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接