问题描述
||
我有一个插件,我想用来启动计算机上已经安装的程序,例如notepad.exe
问题是,Firefox似乎没有在系统PATH中搜索该程序,即
file.initWithPath(\"c:\\\\windows\\\\system32\\\\notepad.exe\");//works
//file.initWithPath(\"notepad.exe\");//does not work
//file.initWithPath(\"%systemroot%\\\\notepad.exe\");//does not work
题:
有没有办法让Firefox在系统PATH中查找程序?
这是我的完整功能
autoStartNotepad:function()
{
// create an nsILocalFile for the executable
var file = Components.classes[\"@mozilla.org/file/local;1\"]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(\"notepad.exe\");//does not work
//but file.initWithPath(\"c:\\\\windows\\\\system32\\\\notepad.exe\");//works
// create an nsIProcess
var process = Components.classes[\"@mozilla.org/process/util;1\"]
.createInstance(Components.interfaces.nsIProcess);
process.init(file);
// Run the process.
// If first param is true,calling thread will be blocked until
// called process terminates.
// Second and third params are used to pass command-line arguments
// to the process.
var args = [];
process.run(false,args,args.length);
}
解决方法
nsIProcess
不会这样做,它只接受完整路径。您需要亲自查看环境变量:
var environment = Components.classes[\"@mozilla.org/process/environment;1\"]
.getService(Components.interfaces.nsIEnvironment);
var path = environment.get(\"PATH\");
var root = environment.get(\"SYSTEMROOT\");
您可以拆分PATH
变量并检查各个目录,也可以使用SYSTEMROOT
变量的值。
您可以运行cmd.exe /c notepad.exe
,但也需要首先定位cmd.exe
。