Reputation: 11220
import 'dart:io';
void main() {
var path = Platform.script.path;
print(path);
}
Output
/C:/Users/user/dart/test/bin/test.dart
But I want get
C:/Users/user/dart/test/bin/test.dart
What is recommended way to get OS specific path ready to use in this OS?
P.S.
If I run test code on different platform I get different result.
So, test.
Runtime: Dart SDK version 1.1.1 (STABLE)
Code:
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}
This behavior prevent me to write cross-platform code.
Upvotes: 3
Views: 2453
Reputation: 11220
This code works on all platforms.
import 'dart:io';
void main() {
var path = Platform.script.toFilePath();
print(path);
var uri = new Uri.file(path);
print(uri.toFilePath());
}
P.S.
Similar exception (Illegal character in path
) can occurs inside Dart SDK (in some cases) when used scheme ""dart-ext":
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';
^
Upvotes: 3
Reputation: 657957
Take a look at the path package import package:path/path.dart
.
I don't have Windows running here so I can't verify anything.
After a brief look I found:
/// 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();
Upvotes: 1