Why is this C# code not working? I am trying to read the output of a shell to TortoiseHG (Mercurial)

戏子无情 提交于 2019-12-06 10:51:05

Your code works for me (tested with TortoiseHg 2.0.2), provided that I pass the full path to the executable:

proc.StartInfo.FileName = "C:\\Program Files (x86)\\TortoiseHg\\hg.exe";

My guess is there is output going to standard error.

This page talks about how to do it:

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.standarderror.aspx

I think you might be needing a

proc.WaitForExit();

before your read to end call? Unless the process is interactive, then you have a different problem.

You might consider handling the Process.OutputDataReceived and ErrorDataReceived events:

proc.ErrorDataReceived += delegate(object o, DataReceivedEventHandler e)
  {
      if (e.Data != null) { /* e.Data is the string from the process */ }
  };
proc.OutputDataReceived += delegate(object o, DataReceivedEventHandler e)
  {
      // ...
  };

Be sure to call proc.BeginErrorReadLine() and proc.BeginOutputReadLine() after starting the process.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!