【问题标题】:Write only the output from the console command只写控制台命令的输出
【发布时间】:2012-12-27 12:52:59
【问题描述】:

我正在尝试构建一个 .net 应用程序,该应用程序将运行一些控制台命令(如运行 phantomJs)并将操作结果返回给我。但默认情况下,我会得到从cmd.exe 开始到关闭它的所有内容。有什么快速修复的想法还是我需要使用正则表达式?

这是我现在的代码:

System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;

System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader sOut = proc.StandardOutput;

System.IO.StreamWriter sIn = proc.StandardInput;

sIn.WriteLine("phantomjs -v");
sIn.WriteLine("EXIT");

proc.Close();

string results = sOut.ReadToEnd().Trim();

sIn.Close();
sOut.Close();

【问题讨论】:

    标签: c# .net console outputstream


    【解决方案1】:

    PhantomJS 是一个可执行文件(根据他们的文档) - 为什么不直接执行它而不是运行 cmd.exe?这将避免 cmd.exe 噪音。

    或者将phantomjs的输出重定向到一个日志文件并加载该日志文件。

    或者,如果您绝对必须使用 cmd.exe 并且无法重定向...我可能会在 phantomjs 周围抛出一些 echo sentinels 作为解析开始/停止点。

    例如,

    echo PARSE START
    runcommand.exe
    echo PARSE STOP
    

    但不要那样做。

    【讨论】:

    • 我已经尝试过new System.Diagnostics.ProcessStartInfo("phantomjs -v") 以及 phantomjs.exe 但它带来了错误:The system cannot find the file specified。还是应该以不同的方式执行?
    • @mike_hornbeck 它只是期待那里的文件名,您需要将参数放在Arguments 属性中。
    【解决方案2】:

    而不是使用不同的流。为什么不使用cmd 作为filename 并将-c "phantomjs -v" 作为参数传递给它。然后使用proc.StandardOutput.ReadToEnd() 抓取控制台中输出的所有内容。这应该忽略不需要的信息,因为它只读取执行命令的输出。

    以下代码可能不起作用,但应该可以为您提供大致的思路。

    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = "cmd";
    psi.Arguments = "/c \"phantomjs -v\"";
    psi.UseShellExecute = false;
    psi.RedirectStandardOutput = true;
    // Optional other options
    
    Process proc = Process.Start(psi);
    
    string output = proc.StandardOutput.ReadToEnd();
    
    proc.WaitForExit();
    

    【讨论】:

    • 使用您的解决方案,我没有得到 phastomjs 版本的输出,而且我仍然需要将 sIn.WriteLine("EXIT"); 写入输入流以关闭命令行。
    • 添加了UseShellExecuteRedirectStandardOutput。这应该足以获得输出。还注意到它应该是 /c 而不是 -c
    【解决方案3】:

    如果您使用的是 unix 机器:

    sIn.WriteLine("phantomjs -v > /dev/null");
    

    窗户:

    sIn.WriteLine("phantomjs -v > NUL");
    

    【讨论】:

      【解决方案4】:

      希望以下内容对您有所帮助!

      {
      
       Process xyProcess = new Process();
      
       xyProcess.StartInfo.FileName = "FilenameYouWant";
       xyProcess.StartInfo.UseShellExecute = false;
       xyProcess.StartInfo.CreateNoWindow = true;
       xyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
       xyProcess.StartInfo.RedirectStandardInput = true;
       xyProcess.StartInfo.RedirectStandardOutput = true;
       xyProcess.StartInfo.RedirectStandardError = true;
      
       xyProcess.StartInfo.Arguments += "any arg1 you want ";
       xyProcess.StartInfo.Arguments += "any arg2 you want ";
      
       xyProcess.EnableRaisingEvents = true;
       xyProcess.OutputDataReceived += process_DataReceived;
      
       // Start the process
       xyProcess.Start();
       xyProcess.BeginErrorReadLine();
       xyProcess.BeginOutputReadLine();
       xyProcess.WaitForExit();
      
      }
      static private void process_DataReceived(object sender, DataReceivedEventArgs e)
      {
          //Catch the process response here
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-05
        • 1970-01-01
        • 2014-07-14
        • 2017-12-21
        相关资源
        最近更新 更多