通过c#进程在批处理文件中运行git fetch会挂起
我试图通过c#运行几个git命令,并继续处理结果输出 我写了这个小方法:通过c#进程在批处理文件中运行git fetch会挂起,c#,git,batch-file,C#,Git,Batch File,我试图通过c#运行几个git命令,并继续处理结果输出 我写了这个小方法: private string runGitCommand(string gitCommand, string path) { ProcessStartInfo processInfo = new ProcessStartInfo(); processInfo.FileName = System.Windows.Forms.Application.StartupPath+"\\gitc
private string runGitCommand(string gitCommand, string path)
{
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.FileName = System.Windows.Forms.Application.StartupPath+"\\gitcommand.bat";
processInfo.WindowStyle = ProcessWindowStyle.Hidden;
processInfo.Arguments = gitCommand;
processInfo.CreateNoWindow = true;
processInfo.UseShellExecute = false;
processInfo.WorkingDirectory = path;
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = processInfo;
string output="";
process.OutputDataReceived += (a, b) => {
Console.WriteLine(b.Data);
output += b.Data;
}
;
process.ErrorDataReceived += (a, b) => Console.WriteLine(b.Data);
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
return output;
}
当然,有些设置是用于调试的(比如不必要的Console.WriteLine()
),但它可以与大多数git命令一起使用,比如status
或rev parse HEAD
这是我使用的批处理文件:
@"C:/Program Files (x86)/Git/bin/git.exe" %1 %2
有两个参数,因为rev parse HEAD
是两个参数。
我在执行status
时没有任何问题,所以这两个参数应该不会有问题
所有操作都与预期完全相同,但当我执行fetch
时,程序挂起
我在cmd中使用了具有相同输入的批处理文件,它确实花费了时间(大约1-2秒),但它退出时没有出现任何问题(没有给出任何输出)
有什么想法吗?问题在于RSA身份验证不起作用。 由于
git fetch
与服务器联系并且git status
not,因此只有手动在cmd(而不是git bash)中键入所使用的命令才能看到此错误
程序正在等待用户输入。正如@ardila所建议的,在C#中使用git时应该使用的。完全取决于回购协议及其配置,但它可能正在等待用户输入。在终端上执行相同的命令(逐字)时会发生什么?您是否收到身份验证提示?您应该将您的评论作为答案发布^^^这正是问题所在,显然,只有与服务器联系的git命令存在RSA身份验证问题(因为出于测试目的,我复制了repo)。所以是的,有一个等待用户输入的过程。知道我如何检查程序是否正在等待用户输入吗?知道我如何检查程序是否正在等待用户输入吗。如果您将此代码编写为快速原型之外的任何东西,我会使用更健壮的东西(例如),这是一种非常迂回的说法“我不知道”;)不要将答案添加到问题中,而是将其作为答案发布,因为这正是他们的目的!回答你自己的问题绝对没有问题,你甚至可以在以后接受你自己的答案。。。