2

我试图让 Phantom.JS 在 Windows 计算机上运行,​​并将它作为 BAT 文件工作。我可以打开一个控制台窗口,启动 BAT 文件,一切似乎都可以正常工作。也就是说,输出对于控制台来说太长了,所以我想在 .NET 控制台应用程序中运行相同的东西。我尝试了很多东西,但一直收到错误消息“phantom.js 未被识别为内部或外部命令”

BAT 文件本身包含以下命令:

phantomjs --config=config.json netsniff.js http://google.com 

这是我的 .NET 代码:

Process compiler = new Process();
compiler.StartInfo.FileName = "cmd.exe"; 
compiler.StartInfo.Arguments = "/C C:\\Test\\getpage.bat";
compiler.StartInfo.UseShellExecute = true;
compiler.Start();

有人可以帮我弄清楚我做错了什么吗?谢谢!

感谢大家为我解决这个问题。这是一个有效的解决方案:

Process compiler = new Process();
compiler.StartInfo.FileName = "cmd.exe";
compiler.StartInfo.WorkingDirectory = "C:\\Test\\";
compiler.StartInfo.Arguments = "/r getpage.bat";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();

File.WriteAllText("C:\\Test\\pageoutput.txt",compiler.StandardOutput.ReadToEnd());

compiler.WaitForExit();
4

1 回答 1

3

您将 bat 位置指定为文件名而不是 cmd.exe

Process compiler = new Process();
compiler.StartInfo.FileName = "C:\\Test\\getpage.bat"; 
compiler.StartInfo.UseShellExecute = true;
compiler.Start();

虽然我没有看到对 bat 文件的具体需求。你可以这样做:

Process compiler = new Process();
compiler.StartInfo.FileName = DirectoryToPhantomJs + "phantomjs"; 
compiler.StartInfo.Arguments = "--config=config.json netsniff.js http://google.com ";
compiler.StartInfo.UseShellExecute = true;
compiler.Start();

第二种方法的好处是您明确指定了 phantom js 的位置,因此您不应该收到“phantom.js 未被识别为内部或外部命令”错误。

于 2012-12-19T17:04:51.783 回答