我正在尝试让 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();
最佳答案
您将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 未被识别为内部或外部命令”错误。
关于.net - 在 .NET 中运行 BAT 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13957389/