2014-01-18 24 views
2
import 'dart:io'; 

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

输出在Dart SDK的Windows中如何在不引导“斜线”的情况下如何获得路径字符串?

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

但我希望得到

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

什么建议的方式来获得准备在这个操作系统使用操作系统特定的路径?

P.S.

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

因此,测试。

运行:飞镖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} 

此行为阻止我w rite跨平台代码。

+0

Platform.script.path'的'的值是正确'/ C :/ Users/user/dart/test/bin/test.dart'。URI是以下文件URI:'file:/// C:/ Users/user/dart/test/bin/test.dart'。 [Windows中的文件URI](http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx)获取更多信息。 – sgjesse

回答

0

看一看路径包import package: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(); 
+0

您可以查看更新因此,package'path'不能解决这个问题 – mezoni

+0

我不明白为什么'path'在这里没有帮助,当路径文档说'windows'使用的样式时,你的windows代码仍然使用'/' '\'绝对路径以驱动器号开头。 –

2

此代码适用于所有平台。

import 'dart:io'; 

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

P.S.

类似的异常使用方案 “” 镖-EXT“时达特SDK内发生(Illegal character in path)可以(在某些情况下):

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'; 
^ 
相关问题