日期:2014-05-16  浏览次数:20401 次

如何使用js调用系统命令
1、第一点,必须把IE浏览器的安全性设置得非常低,允许执行activx控件。不然会返回这个错误:"Automation 服务器不能创建对象"
2、只能在IE6,7,8下面执行。

举例如下:

事例1:
var cmd = new ActiveXObject("WScript.Shell");
var command = "echo Hello World!" //这里是执行的DOS命令
cmd.run("cmd.exe /k "+command);
cmd = null;

事例2:
var objShell = new ActiveXObject("wscript.shell");
objShell.Run("c:\\windows\\system32\\arp.exe -a");
objShell=null;


在JS也可以把它封装成函数:



<SCRIPT language=JavaScript>      
function   RunCmd(strPath) 
{        
    try {      
        var objShell  =  new ActiveXObject( "wscript.shell ");      
        objShell.Run(strPath);      
        objShell = null;      
    }      
    catch (e){alert( '找不到文件: '+strPath)      
       
    }      
}      
</SCRIPT>