0

我正在使用Mozilla的addon sdk进行开发,并且需要在本地系统上创建一个文件。
目前我使用下面的声明,但觉得它可能不包括所有平台。
在Windows 7和Windows XP上运行的语句返回:创建一个独立于平台的路径字符串

console.log(system.platform); 
winnt 

在Linux上运行,它返回:

console.log(system.platform); 
linux 

是否有创建fullPath串一个更可靠的方法,而无需检查内容system.platform

pathToFile = Cc["@mozilla.org/file/directory_service;1"] 
    .getService(Ci.nsIProperties).get("Home", Ci.nsIFile).path; 

if (system.platform.indexOf("win") == 0) { 
    fileSeparator = "\"; 

}else{ 
    fileSeparator = "/"; 
} 

fullPath=pathToFile + fileSeparator + 'myFile.txt' 

回答

1

只是一点点modfication你的代码应该做的伎俩

var file = Cc["@mozilla.org/file/directory_service;1"] 
       .getService(Ci.nsIProperties).get("Home", Ci.nsIFile); 

file.append("myFile.txt"); 

var fullPath = file.path; 
0

我想指出,以@卡希夫的答案的替代品。

使用FileUtils.getFile(),这只是一个方便的功能,实质上是做多个.append() s,每个零件数组中的一个零件。

Cu.import("resource://gre/modules/FileUtils.jsm"); 
var file = FileUtils.getFile("Home", ["myFile.txt"]); 
var path = file.path;