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

China☆狼群 提交于 2019-12-07 21:13:58

问题


I am trying to get mercurial to run in a shell from my C# wpf application. My purpose is to retrieve the output into a string so that I can parse it.

Unfortunately for me, it seems that hg.exe (from tortoiseHg), does not return anything via the code below. Other .exe's appear to work, as seen in the comments below;

My Code is below;

`

        string workingDir = "";
        string filename = "";
        string param = "";

        //This works
        workingDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        filename = "unrar.exe";
        param = "";

        //this works
        workingDir = "c:\\program files\\WinRar";
        filename = "unrar.exe";
        param = "";

        //this works
        workingDir = "C:\\Program Files (x86)\\TortoiseHg";
        filename = "docdiff.exe";
        param = "";

        //this does not work. I get a null returned. Why?
        workingDir = "C:\\Program Files (x86)\\TortoiseHg";
        filename = "hg.exe";
        param = "";

        //this does not work. I get a null returned. Why?
        workingDir = "C:\\Program Files (x86)\\TortoiseHg";
        filename = "hg.exe";
        param = "help";

        string retVal = "";
        System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.StartInfo.WorkingDirectory = workingDir;            
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.FileName = filename;
        proc.StartInfo.Arguments = param;
        proc.Start();

        System.IO.StreamReader reader = proc.StandardOutput;
        retVal = reader.ReadToEnd();
        System.Windows.MessageBox.Show(retVal);`

If anyone could suggest why this code does not work, or alternatively another method of retrieving the output of mercurial command lines, I would be very appreciative.

Thank you


回答1:


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";



回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/4139040/why-is-this-c-sharp-code-not-working-i-am-trying-to-read-the-output-of-a-shell

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