gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:39 24 4
gpt4 key购买 nike

我试图让 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";

关于c# - 为什么此 C# 代码不起作用?我正在尝试将 shell 的输出读取到 TortoiseHG (Mercurial),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4139040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com