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

2024-04-24

我正在尝试让 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 命令行输出的方法,我将非常感激。

谢谢


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

proc.StartInfo.FileName = "C:\\Program Files (x86)\\TortoiseHg\\hg.exe";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐