.net - 在 .NET 中运行 BAT 程序

标签 .net console-application phantomjs

我正在尝试让 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/

相关文章:

c# - JIT 编译器是否优化(内联)不必要的变量声明?

.net - 正则表达式太松散

c# - 控制台窗口关闭时是否可以订阅事件?

vb.net - 如何防止 VB.NET 中的控制台窗口被调整大小?

r - 使用 phantomjs 通过 Rselenium 发送 key

.net - .net Entity Framework 与 LinqToSql 相比如何?

c# - 需要帮助来决定什么是跟踪系统 (.NET) 的最佳解决方案

c# - 带服务和控制台的 System.Threading.Mutex

r - 在knitr中使用webshot

javascript - 从 karma 服务器提供脚本文件