为什么这段 C# 代码不起作用?我正在尝试将 shell 的输出读取到 TortoiseHG (Mercurial)

发布于 2024-10-01 19:27:55 字数 1689 浏览 6 评论 0原文

我正在尝试让 Mercurial 在我的 C# wpf 应用程序的 shell 中运行。我的目的是将输出检索到字符串中,以便我可以解析它。

对我来说不幸的是,hg.exe(来自 tortoiseHg)似乎不会通过下面的代码返回任何内容。其他 .exe 似乎可以工作,如下面的评论所示;

我的代码如下;

如果

        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);`

有人能提出为什么这段代码不起作用,或者提供另一种检索 Mercurial 命令行输出的方法,我将非常感激。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

〗斷ホ乔殘χμё〖 2024-10-08 19:27:56

我的猜测是有输出进入标准错误。

本页讨论如何执行此操作:

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

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

述情 2024-10-08 19:27:56

我认为您可能需要

proc.WaitForExit();

在阅读结束之前进行通话?除非该过程是交互式的,否则您会遇到不同的问题。

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.

摇划花蜜的午后 2024-10-08 19:27:56

您可能会考虑处理 Process.OutputDataReceived 和 ErrorDataReceived 事件:

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)
  {
      // ...
  };

请务必在启动进程后调用 proc.BeginErrorReadLine()proc.BeginOutputReadLine()

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.

冬天的雪花 2024-10-08 19:27:55

如果我传递可执行文件的完整路径,您的代码对我有用(使用 TortoiseHg 2.0.2 进行测试):

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

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文