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;