首页 > 解决方案 > 从 C# 应用程序调用 git clone 不会返回输出数据

问题描述

我通过创建一个新进程从 C# 应用程序调用 git clone,它成功调用 git 但没有输出返回给我的委托。这意味着随着流程的进行,我无法向用户提供任何更新。根本没有调用 Clone_DataOutputReceived 方法。

public void CloneRepo(string repoUrl, string dataPath)
{
   var process = new Process();
   process.StartInfo.FileName = @"git.exe";
   process.StartInfo.Arguments = $"clone {repoUrl}";
   process.StartInfo.WorkingDirectory = dataPath;

   process.OutputDataReceived += Clone_DataOutputReceived;

   process.StartInfo.RedirectStandardOutput = true;
   process.StartInfo.RedirectStandardError = true;
   process.StartInfo.UseShellExecute = false;
   process.StartInfo.CreateNoWindow = true;

   process.Start();
   process.BeginOutputReadLine();
   process.WaitForExit();
   process.CancelOutputRead();
}


private void Clone_DataOutputReceived(object sender, DataReceivedEventArgs e)
{
   // This method is not being called
   // Expecting d.Data to be populated with the line from the output
}

标签: c#git

解决方案


我认为您想将 FileName 设置为“cmd.exe”,然后向 cmd 实例发出 process.SendCommand("git.exe") 。


推荐阅读