这里我试图检查文件cmd.bat是否存在于临时文件夹中然后不创建新文件夹,否则在临时文件夹中创建一个cmd.bat。但是临时文件夹路径“%temp%\ cmd.bat”有问题,它显示错误:
Timestamp: 10/31/2012 3:21:40 AM
Error: NS_ERROR_FILE_UNRECOGNIZED_PATH: Component returned failure code: 0x80520001 (NS_ERROR_FILE_UNRECOGNIZED_PATH) [nsILocalFile.initWithPath]
Source File: chrome://myext/content/overlay.js
Line: 9
我的代码:
var chk_file = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
chk_file.initWithPath("%temp%\cmd.bat");
if ( chk_file.exists() == false ) {
Components.utils.import("resource://gre/modules/FileUtils.jsm");
var file = FileUtils.getFile("TmpD", ["cmd.bat"]);
file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, FileUtils.PERMS_FILE);
}
else
{
alert("file already exists");
}
答案 0 :(得分:1)
initWithPath仅采用完整的系统路径。
使用此命令转到temp,然后尝试
var environment = Components.classes["@mozilla.org/process/environment;1"]
.getService(Components.interfaces.nsIEnvironment);
var path = environment.get("TEMP");