开发者

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

开发者 https://www.devze.com 2023-01-24 11:32 出处:网络
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.

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 S开发者_如何转开发ystem.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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号