我想从C#运行git命令。下面是我编写的代码,它确实执行了git命令,但是我无法捕获返回值。当我从命令行手动运行它时,这是我得到的输出。

当我从程序运行时,我得到的唯一是
Cloning into 'testrep'...
Run Code Online (Sandbox Code Playgroud)
其余信息未捕获,但命令已成功执行。
class Program
{
static void Main(string[] args)
{
ProcessStartInfo startInfo = new ProcessStartInfo("git.exe");
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = @"D:\testrep";
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.Arguments = "clone http://tk1:tk1@localhost/testrep.git";
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
List<string> output = new List<string>();
string lineVal = process.StandardOutput.ReadLine();
while (lineVal != null)
{
output.Add(lineVal);
lineVal = process.StandardOutput.ReadLine();
}
int val = output.Count();
process.WaitForExit();
}
}
Run Code Online (Sandbox Code Playgroud)
从git clone的手册页:
--progress 当附加到终端时,默认情况下在标准错误流上报告进度状态,除非指定 -q 。即使标准错误流未定向到终端,此标志也会强制执行进度状态。
交互运行时输出中的最后三行将git clone发送到标准错误,而不是标准输出。但是,当您从程序运行命令时,它们不会显示在那里,因为它不是交互式终端。您可以强制它们出现,但输出不会是任何可供程序解析的内容(大量的\rs 来更新进度值)。
您最好根本不解析字符串输出,而是查看 的整数返回值git clone。如果它不为零,则表示出现错误(并且标准错误中可能会出现一些可以向用户显示的内容)。
| 归档时间: |
|
| 查看次数: |
7388 次 |
| 最近记录: |